By Smurfette


2013-07-25 15:58:43 8 Comments

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?

4 comments

@Cristian Lupascu 2013-07-25 16:02:28

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");

Note how an extra \ before \s changes the output.

@schlicht 2013-07-25 16:04:31

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

@Quentin 2013-07-25 16:03:05

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.

@Wiktor Stribi┼╝ew 2018-09-27 15:28:34

That pertains to both regular string literals as well as template string literals.

@Joe Enos 2013-07-25 16:02:19

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

Related Questions

Sponsored Content

14 Answered Questions

9 Answered Questions

[SOLVED] Why does ++[[]][+[]]+[+[]] return the string "10"?

  • 2011-08-26 08:46:14
  • JohnJohnGa
  • 187867 View
  • 1525 Score
  • 9 Answer
  • Tags:   javascript syntax

26 Answered Questions

[SOLVED] Why is using "for...in" with array iteration a bad idea?

35 Answered Questions

[SOLVED] RegEx match open tags except XHTML self-contained tags

  • 2009-11-13 22:38:26
  • Jeff
  • 2474062 View
  • 1324 Score
  • 35 Answer
  • Tags:   html regex xhtml

6 Answered Questions

[SOLVED] Why does Google prepend while(1); to their JSON responses?

5 Answered Questions

[SOLVED] What is TypeScript and why would I use it in place of JavaScript?

  • 2012-10-02 16:37:58
  • Mohammed Thabet
  • 424118 View
  • 1443 Score
  • 5 Answer
  • Tags:   javascript typescript

40 Answered Questions

[SOLVED] When to use double or single quotes in JavaScript?

1 Answered Questions

[SOLVED] Escape string for use in Javascript regex

39 Answered Questions

[SOLVED] A comprehensive regex for phone number validation

1 Answered Questions

Sponsored Content