Why do regex constructors need to be double escaped?
double slash in regex
regex double backslash
regex replace double backslash
regex escape backslash
regex double forward slash
regex escape dot
regex escape comma
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?
You are constructing the regular expression by passing a string to the RegExp constructor.
\ is an escape character in string literals.
\ is consumed by the string literal parsing…
const foo = "foo"; const string = '(\s|^)' + foo; console.log(string);
So when you generate a string by saying
var someString = '(\\s|^)', what you're really doing is creating an actual string with the value
Regular Expressions Cookbook, Regular expressions can also be created by using the RegExp constructor, which there are some instances in which you need to double-escape characters. In regex the \ is an escape which tells regex that a special character follows. Because you are using it in a string literal you need to escape the \ with \. This function is inefficient because each time it is called it is converting a string to a regex and then it is compiling that regex.
The Regex needs a string representation of
Here's a live example to illustrate why
"\s" is not enough:
alert("One backslash: \s\nDouble backslashes: \\s");
\ 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 \.
Class: Regexp (Ruby 2.7.0), Question: How do I match a backslash using regular expressions? When calling a RegExp constructor, you have to double each of the two backslashes in the Generally, there's not much good reason to use new RegExp unless you need to dynamically create a regular expression from existing variables. Otherwise, you should use regex literals instead, which do not require double-escaping of metacharacters, and do not require writing out String.raw to keep the pattern readable:
As has been said, inside a string literal, a backslash indicates an escape sequence, rather than a literal backslash character, but the RegExp constructor often needs literal backslash characters in the string passed to it, so the code should have
\\s to represent a literal backslash, in most cases.
A problem is that double-escaping metacharacters is tedious. There is one way to pass a string to
new RegExp without having to double escape them: use the
String.raw template tag, an ES6 feature, which allows you to write a string that will be parsed by the interpreter verbatim, without any parsing of escape sequences. For example:
console.log('\\'.length); // length 1: an escaped backslash console.log(`\\`.length); // length 1: an escaped backslash console.log(String.raw`\\`.length); // length 2: no escaping in String.raw!
String literal, When using the RegExp constructor, the pattern is written as a normal string, Regular expression objects have a number of methods. date and time pattern that allows both single- and double-digit days, months, and hours g, "\\$&"); let regexp = new RegExp("\\b" + escaped + "\\b", "gi"); console.log(text.replace(regexp, You need the \\ because the expression between tour quotes is interpreted twice: once and first as a character string (in which \( is illegal but \\ is legal) and then as a regular expression in which you want to match a literal ( and ) which must be escaped in the regular expression since they are meta characters.
[PDF] Regular Expressions: The Complete Tutorial, A string literal or anonymous string is a type of literal in programming for the representation of a Double quotations are the most common quoting delimiters used: "Hi There!" In terms of regular expressions, a basic quoted string literal is given as: "[^"]*" This avoids the need for escaping, and yields more legible strings. The Regex.Escape method has many possible uses. Because you can always escape strings in the source code that you type in, you will not need to use it in most programs. Tip: If your program retrieves external input, you can use Escape to eliminate the chance that characters will be incorrectly used.
class Regexp - Documentation for Ruby 2.0.0, A regular expression (regex or regexp for short) is a special text string for describing a search pattern. Matches are indicated by double quotation marks, with the So you generally do not need to escape it with a backslash, though you can do so if expression passed to the Regex() constructor in btnRegexObj_Click. This is one of the techniques that we can use to escape metacharacters in a regular expression. However, we know that the backslash character is an escape character in Java String literals as well. Therefore, we need to double the backslash character when using it to precede any character (including the \ character itself).
- Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
- Related: stackoverflow.com/a/37329801/1225328.
- That pertains to both regular string literals as well as template string literals.