0

I'm trying to match the phrase "/$" using the RegExp constructor, but no amount of escaping seems to help. Am is missing something?

RegExp("\/\$").test("/$")
// false
RegExp("/$").test("/$")
// false
RegExp("\/$").test("/$")
// false
RegExp("/\$").test("/$")
// false
2
  • 1
    RegExp("\\$").test("$") Commented Jul 19, 2016 at 5:31
  • RegExp("\\/\\$").test("/$") Commented Jul 19, 2016 at 5:31

1 Answer 1

2

You need to use \\ instead of \ and there is no need to escape / or use regex /\/\$/ directly. Check RegExp documentation for more info.

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

For example, /\w+/ is equivalent to new RegExp('\\w+')

console.log(
  RegExp("/\\$").test("/$")
)

//or

console.log(
  /\/\$/.test("/$")
)

Refer : Javascript regular expression - string to RegEx object

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.