I came across this problem when trying to stop a single quote (')
being matched in a string.
Here is a snippet from a console session in Chrome. Params is the regex I'm trying to match (no single or double quotes should be allowed?). I would have expected the first two execs to find a match and the second two to fail due to the single quote in the text.
Suppose this raises two questions:
- Why does the literal behave differently to the variable?
- Why does the third exec find a match when there should be no match on the single quote?
thanks
> params
>
>> "^[a-zA-Z0-9 -_/&,()\[\];:+~.!\\]*$"
>
>
> new RegExp(params).exec("some string")
>> ["some string"]
>
> new RegExp("^[a-zA-Z0-9 -_/&,()\[\];:+~.!\\]*$").exec("some string")
>> null
>
>
> new RegExp(params).exec("some string's")
>> ["some string's"]
>
> new RegExp("^[a-zA-Z0-9 -_/&,()\[\];:+~.!\\]*$").exec("some string's")
>> null