A Complete Dot in JavaScript Regular Expressions

This is a short post to share a technique I find useful with regular expressions in JavaScript.

For the most part JavaScript uses Perl’s syntax. However, it does not support the /s modifier. Normally the . meta-character will match anything except a newline; the /s modifier in Perl causes the dot to match everything, including newlines. JavaScript does not support this modifier.

We can get around this by using a character class like so: [\s\S]. Common character classes often have short-hand names. For example, \s is often used to mean ‘any whitespace character’, e.g. as if it were [ \t\r\n]. The capital letter versions represent the inverse. That means \S matches any character which is not whitespace. Combining them in JavaScript allows us to match any character whatsoever; the character class [\s\S] means, “Match anything that is either whitespace or is not whitespace,” and that covers all characters.

So JavaScript does not have Perl’s /s modifier, which would allow us to simply use the . meta-character. But with a small character class we can achieve the same result.

Advertisements

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s