1

Reading this document https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp

"When using the constructor function, the normal string escape rules (preceding special characters with \ when included in a string) are necessary."

So why is there no difference between these two uses of RegExp()?

"a/b/c".match(new RegExp("\/", "g")) // (2) [ "/", "/" ]
"a/b/c".match(new RegExp("/", "g"))  // (2) [ "/", "/" ]

Is the document incorrect or am I missing something?

The possible duplicate question indicates that forward slashes must be escaped as literals. Using a string, the example above shows this is not the case given the use case of a string with the constructor function. This question is specifically about using a string with the constructor function.

So based on the answer below, the difference appears to be with literals, both the " and the / need to be escaped, but when using a string only the " needs to be escaped.

ciso
  • 2,887
  • 6
  • 33
  • 58
  • / is not a special character in Regex. It is special in js to determine in regexp objects (that is literal /someregex/) where the delimiter ends. So in your case you are not using regexp object with literal forward slashes, escaping is not needed. – ibrahim tanyalcin May 13 '18 at 22:51
  • Possible duplicate of [Matching a Forward Slash with a regex](https://stackoverflow.com/questions/16657152/matching-a-forward-slash-with-a-regex) – ibrahim tanyalcin May 13 '18 at 22:53
  • That is when you are using regexp with **literal** foward slashes. In declaring regexp with the constructor you are NOT required. using new Regexp and using `"a/b/c".match(/\//g)` are DIFFERENT!!! – ibrahim tanyalcin May 13 '18 at 22:57
  • It does address it however you cannot comprehend that declaring a regexp using the constructor and literal are **different**. – ibrahim tanyalcin May 13 '18 at 23:00
  • @ibowankenobi That is the point, isn't it? This is why it is different. A literal and a string have slightly different rules. – ciso May 13 '18 at 23:02
  • Why is it different???? Because by design, the first option (literal) has to know when the literal ends. The delimiter is forward slash, so you need to escape the forward slash. But when using the constructor the delimiter is IMPLIED, so you do not need to escape it. What have I been saying for 3 comments????? – ibrahim tanyalcin May 13 '18 at 23:04
  • 1
    I'm not sure the answer, or what has been said here is really saying why -> `why is there no difference`.. There is no difference because this has nothing to do with RegEx, it's to do with javascript string literals,.. `"\/" === "/"` is true, because it's JS escaping and not Regex escaping. – Keith May 13 '18 at 23:26
  • 1
    A regex literal `/abc/` is comparable to a string literal `"abc"`. In both cases you have delimiters that mark the beginning and end of the literal - for a regex it's `/`, for a string it's `"`. If you want to include the same delimiter *inside* the literal, you have to escape it - `/ab\/c/` producing a regex that matches `ab/c` and `"ab\"c"` is a string containing `ab"c`. The constructor `new RegExp("abc")` is the same as the literal `/abc/` - the string passed is the content *inside* the delimiters, they are omitted, and `/` is not treated differently. – VLAZ May 13 '18 at 23:38

1 Answers1

4

Forward slashes are only special characters in that they designate the syntactical beginning and end of a literal regular expression in Javascript. But when you use the constructor, that's not needed, because the first parameter provided to new RegExp is the (entire) string to construct the regular expression from.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • So a forward slash is not a special character when using a string with RegExp()? – ciso May 13 '18 at 22:53
  • 2
    Right - the escape of forward slashes in RE literals is a *Javascript* indicator that the end of the regular expression has not been reached. When you use the constructor, that's not needed, because the entire argument itself represents the string that the RE should be constructed from. – CertainPerformance May 13 '18 at 23:00