Max Koretskyi
Max Koretskyi

Reputation: 105439

regex returns false when should return true

I have this regex:

regex = new RegExp("\${")

and this string:

path = "${wf:conf('oozie.wf.application.path')}/workflows/core"

When I test the string against the regexp:

regex .test(path)

it returns false, why?

Upvotes: 0

Views: 97

Answers (3)

Amadan
Amadan

Reputation: 198294

Can you please explain why should I use double slash? I use one to escape $, why second?

Let us take another example. "\n" is an escape-sequence in a string, producing a newline character. In order to produce the sequence \n (backslash, then n) in a string, you need to escape the backslash: "\\n". To produce a regular expression that matches the string \n (backslash, n), you can't use new RegExp("\\n"), since that will make a new regexp of \ and n (result of string escaping); but regexp again interprets \ and n as newline. So you need double escape: once to tell string that you want a literal backslash, and once more to convince regexp that you want a literal backslash as well. So, to match the backslash followed by n, you need to do this:

new Regexp("\\\\n")

which is equivalent to

/\\n/

which will match the string in foo if

console.log(foo)
// => \n

In your example, it's not the backslash that is escaped, but a dollar; only regexp needs the escape on that. But you need to be sure that the backslash you escape the dollar with survives the string literal's escape mechanism:

"\\$"

will contain two characters: backslash and $, and regexp constructed form it will be equivalent to /\$/. But if you start with the string "\$", it is equivalent to "$", since dollars don't need escaping in regular strings; and a regexp made from that will match the end of the line, and not a dollar sign.

Upvotes: 1

Felix Kling
Felix Kling

Reputation: 816262

You regex is equivalent to ${. I.e. you are looking for the end of the string followed by a { which will never match.

Just like in regular expressions, you use \ in string literals to escape special characters or create special escape sequences. E.g.

> 'foo\'bar'
"foo'bar"

However, if you are trying to escape a character that is not special or creating an invalid escape sequence, the backslash is just discarded:

> 'foob\ar'
"foobar"

That means, if you use a string literal to construct the regular expression, and you want to "forward" the backslash to the expression, you have to escape it as well:

new RegExp("\\${")

You can just create the expressions in the console and convince yourself:

> RegExp("\${")
/${/

> /${/
/${/

> RegExp("\\${")
/\${/

> /\${/
/\${/

It really is simpler to use a regex literal:

var regex = /\${/;

Upvotes: 4

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use double \ to create \${ as \ is the string literal escape character.

You need a string \${ as the regex, in string literal to represent a \, you need to use \\ as a single \ will act like a escape notation.

regex = new RegExp("\\${")

Upvotes: 1

Related Questions