Back to Top

Saturday, January 03, 2009

Javascript regex quirk

When I've written the SMOG analyzer javascript I found a quirk of javascript and this recent post inspired me to share it:

The javascript regex specification doesn't have the s modifier. This is necessary when you want to match multiple lines with a construct like .*. The suggested workaround I found was to specify the [\s\S] character class which means "any space or non-space character".

BTW, I find that the multi-line / single-line name for the /m and /s modifiers is somewhat a misnomer, since it leads you to believe that you can't use them together (how can something be a single line and multiple lines the same time?), however this is not true, since they refer to different elements (^ and $ versus .), which means that you can use and sometimes you need to use them.

0 comments:

Post a Comment

You can use some HTML tags, such as <b>, <i>, <a>. Comments are moderated, so there will be a delay until the comment appears. However if you comment, I follow.