Why do regex constructors need to be double escaped?

javascript regex escape
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.

The \ is consumed by the string literal parsing…

const foo = "foo";
const string = '(\s|^)' + foo;
console.log(string);

Professional JavaScript for Web Developers, Regular expressions can also be created by using the RegExp constructor, which there are some instances in which you need to double-escape characters. Javascript Regex object and the dollar symbol [duplicate] Ask Question Asked 11 years, 2 months ago. Why do regex constructors need to be double escaped?

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|^).

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 \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:          \s\nDouble backslashes: \\s");

Backslash in JavaScript Regular Expressions, But if you want to hardcode the regular expression into your source code, you C# In C#, you can pass literal regular expressions to the Regex() constructor, and double-quoted strings, double quotes and backslashes must be escaped with  Dealing with Escape Sequences (\) Escape sequences are a little tricky in C++ regular expressions, because they occur in two contexts. C++ assigns special meaning to the backslash within a string literal and requires it to be escaped to be read as an actual backslash: To represent a single backslash, it’s necessary to place double backslashes (\\) in the source code.

\ 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!

Regular Expressions :: Eloquent JavaScript, literals, and by the ::new constructor. When one operand is a regular expression and the other is a string then the They have a specific meaning when appearing in a pattern. Patterns behave like double-quoted strings and can contain the same backslash escapes (the meaning of \s is different, however, see below). The regular expression engine must compile a particular pattern before the pattern can be used. Because Regex objects are immutable, this is a one-time procedure that occurs when a Regex class constructor or a static method is called. To eliminate the need to repeatedly compile a single regular expression,

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).

Comments
  • Remember, it's not that Java or the Regexp constructor need clarification, it's the compiler (or parser).
  • 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|^)/
  • Related: stackoverflow.com/a/37329801/1225328.
  • That pertains to both regular string literals as well as template string literals.