Why do regex constructors need to be double escaped?
In the regex below, s
denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.
But this is not the case as double escapes are required.
Why is this?
var res = new RegExp('(\s|^)' + foo).test(moo);
Is there a concrete example of how a single escape could be mis-interpreted as something else?
javascript
add a comment |
In the regex below, s
denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.
But this is not the case as double escapes are required.
Why is this?
var res = new RegExp('(\s|^)' + foo).test(moo);
Is there a concrete example of how a single escape could be mis-interpreted as something else?
javascript
Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
– GJK
Jul 25 '13 at 16:04
1
To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect:/(s|^)/
– Dan Tao
Jul 25 '13 at 16:05
Related: stackoverflow.com/a/37329801/1225328.
– sp00m
Jul 30 at 9:00
add a comment |
In the regex below, s
denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.
But this is not the case as double escapes are required.
Why is this?
var res = new RegExp('(\s|^)' + foo).test(moo);
Is there a concrete example of how a single escape could be mis-interpreted as something else?
javascript
In the regex below, s
denotes a space character. I imagine the regex parser, is going through the string and sees and knows that the next character is special.
But this is not the case as double escapes are required.
Why is this?
var res = new RegExp('(\s|^)' + foo).test(moo);
Is there a concrete example of how a single escape could be mis-interpreted as something else?
javascript
javascript
asked Jul 25 '13 at 15:58
Smurfette
5351615
5351615
Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
– GJK
Jul 25 '13 at 16:04
1
To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect:/(s|^)/
– Dan Tao
Jul 25 '13 at 16:05
Related: stackoverflow.com/a/37329801/1225328.
– sp00m
Jul 30 at 9:00
add a comment |
Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
– GJK
Jul 25 '13 at 16:04
1
To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect:/(s|^)/
– Dan Tao
Jul 25 '13 at 16:05
Related: stackoverflow.com/a/37329801/1225328.
– sp00m
Jul 30 at 9:00
Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
– GJK
Jul 25 '13 at 16:04
Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
– GJK
Jul 25 '13 at 16:04
1
1
To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect:
/(s|^)/
– Dan Tao
Jul 25 '13 at 16:05
To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect:
/(s|^)/
– Dan Tao
Jul 25 '13 at 16:05
Related: stackoverflow.com/a/37329801/1225328.
– sp00m
Jul 30 at 9:00
Related: stackoverflow.com/a/37329801/1225328.
– sp00m
Jul 30 at 9:00
add a comment |
4 Answers
4
active
oldest
votes
You are constructing the regular expression by passing a string to the RegExp constructor.
You need to escape the so that your string literal can express it as data before you transform it into a regular expression.
That pertains to both regular string literals as well as template string literals.
– Wiktor Stribiżew
Sep 27 at 15:28
add a comment |
Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t
, n
, "
, etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.
So when you generate a string by saying var someString = '(\s|^)'
, what you're really doing is creating an actual string with the value (s|^)
.
add a comment |
The Regex needs a string representation of s
, which in JavaScript can be produced using the literal "\s"
.
Here's a live example to illustrate why "s"
is not enough:
alert("One backslash: snDouble backslashes: \s");
Note how an extra before
s
changes the output.
add a comment |
is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .
EDIT: Even had to do it here, because \ in my answer turned to .
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17863066%2fwhy-do-regex-constructors-need-to-be-double-escaped%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are constructing the regular expression by passing a string to the RegExp constructor.
You need to escape the so that your string literal can express it as data before you transform it into a regular expression.
That pertains to both regular string literals as well as template string literals.
– Wiktor Stribiżew
Sep 27 at 15:28
add a comment |
You are constructing the regular expression by passing a string to the RegExp constructor.
You need to escape the so that your string literal can express it as data before you transform it into a regular expression.
That pertains to both regular string literals as well as template string literals.
– Wiktor Stribiżew
Sep 27 at 15:28
add a comment |
You are constructing the regular expression by passing a string to the RegExp constructor.
You need to escape the so that your string literal can express it as data before you transform it into a regular expression.
You are constructing the regular expression by passing a string to the RegExp constructor.
You need to escape the so that your string literal can express it as data before you transform it into a regular expression.
answered Jul 25 '13 at 16:03
Quentin
638k718611031
638k718611031
That pertains to both regular string literals as well as template string literals.
– Wiktor Stribiżew
Sep 27 at 15:28
add a comment |
That pertains to both regular string literals as well as template string literals.
– Wiktor Stribiżew
Sep 27 at 15:28
That pertains to both regular string literals as well as template string literals.
– Wiktor Stribiżew
Sep 27 at 15:28
That pertains to both regular string literals as well as template string literals.
– Wiktor Stribiżew
Sep 27 at 15:28
add a comment |
Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t
, n
, "
, etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.
So when you generate a string by saying var someString = '(\s|^)'
, what you're really doing is creating an actual string with the value (s|^)
.
add a comment |
Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t
, n
, "
, etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.
So when you generate a string by saying var someString = '(\s|^)'
, what you're really doing is creating an actual string with the value (s|^)
.
add a comment |
Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t
, n
, "
, etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.
So when you generate a string by saying var someString = '(\s|^)'
, what you're really doing is creating an actual string with the value (s|^)
.
Inside the code where you're creating a string, the backslash is a javascript escape character first, which means the escape sequences like t
, n
, "
, etc. will be translated into their javascript counterpart (tab, newline, quote, etc.), and that will be made a part of the string. Double-backslash represents a single backslash in the actual string itself, so if you want a backslash in the string, you escape that first.
So when you generate a string by saying var someString = '(\s|^)'
, what you're really doing is creating an actual string with the value (s|^)
.
answered Jul 25 '13 at 16:02
Joe Enos
30.5k1059122
30.5k1059122
add a comment |
add a comment |
The Regex needs a string representation of s
, which in JavaScript can be produced using the literal "\s"
.
Here's a live example to illustrate why "s"
is not enough:
alert("One backslash: snDouble backslashes: \s");
Note how an extra before
s
changes the output.
add a comment |
The Regex needs a string representation of s
, which in JavaScript can be produced using the literal "\s"
.
Here's a live example to illustrate why "s"
is not enough:
alert("One backslash: snDouble backslashes: \s");
Note how an extra before
s
changes the output.
add a comment |
The Regex needs a string representation of s
, which in JavaScript can be produced using the literal "\s"
.
Here's a live example to illustrate why "s"
is not enough:
alert("One backslash: snDouble backslashes: \s");
Note how an extra before
s
changes the output.
The Regex needs a string representation of s
, which in JavaScript can be produced using the literal "\s"
.
Here's a live example to illustrate why "s"
is not enough:
alert("One backslash: snDouble backslashes: \s");
Note how an extra before
s
changes the output.
alert("One backslash: snDouble backslashes: \s");
alert("One backslash: snDouble backslashes: \s");
edited Sep 22 '16 at 7:40
Wiktor Stribiżew
307k16126202
307k16126202
answered Jul 25 '13 at 16:02
Cristian Lupascu
27.3k1075115
27.3k1075115
add a comment |
add a comment |
is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .
EDIT: Even had to do it here, because \ in my answer turned to .
add a comment |
is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .
EDIT: Even had to do it here, because \ in my answer turned to .
add a comment |
is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .
EDIT: Even had to do it here, because \ in my answer turned to .
is used in Strings to escape special characters. If you want a backslash in your string (e.g. for the in s) you have to escape it via a backslash. So becomes \ .
EDIT: Even had to do it here, because \ in my answer turned to .
edited Aug 27 '13 at 16:58
answered Jul 25 '13 at 16:04
schlicht
3,4271920
3,4271920
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17863066%2fwhy-do-regex-constructors-need-to-be-double-escaped%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
– GJK
Jul 25 '13 at 16:04
1
To add to the already-correct answers: note that if you write a RegExp literal in JavaScript, you don't need to escape the backslash, as you would suspect:
/(s|^)/
– Dan Tao
Jul 25 '13 at 16:05
Related: stackoverflow.com/a/37329801/1225328.
– sp00m
Jul 30 at 9:00