1

Chrome 9.0.597.83 beta on Ubuntu 10.10:

enter image description here

As you can see, the pattern doesn't work when passed in as a string, but works fine when passed in as a RegExp object. Why is this? I need to be able to pass it in as a string so that I can manipulate it before performing the match.


Note: I just posted a question very similar to this... and thought I was doing something wrong and deleted the question. Then when I tried something slightly different, I got the problem again.

2
  • Now I'm getting the problem too, but only with "e c". Commented Feb 6, 2011 at 22:26
  • @Bolt: Yeah, me too now. Commented Feb 6, 2011 at 22:26

1 Answer 1

5

The problem is with the backslash \. This is escaping the W character. In a string, \W evaluates to W. You need to escape the backslash:

"e c".match(new RegExp('(?:^|\\W)c(?:\\W|$)'))
Sign up to request clarification or add additional context in comments.

2 Comments

[Facepalm] Why didn't I think of that? Good job.
@George It took doing console.log(new RegExp('(?:^|\W)c(?:\W|$)')) to make me figure it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.