Javascript regex with literals and single quotes -


i came across problem when trying stop single quote (') being matched in string.

here snippet console session in chrome. params regex i'm trying match (no single or double quotes should allowed?). have expected first 2 execs find match , second 2 fail due single quote in text.

suppose raises 2 questions:

  1. why literal behave differently variable?
  2. why third exec find match when there should no match on 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 

^[a-za-z0-9 _/&,()[];:+~.!\-]*$ 

always keep - @ end or escape avoid forming invalid range.

here - forms range space 32 _ 95.' 39 falls in between invalid range , match.


Comments