Matching newlines in JavaScript
Just a quick note: the . character in a JavaScript regular expression will never match a newline character. If you want to match any character including newlines you can use the [\s\S] character class instead, which means “any character that’s either whitespace or not whitespace”.
This differs from both Python and Perl, where regular expression flags can be used to alter the behaviour of the . character (re.DOTALL and /s respectively).
This tip courtesy of the denizens of #javascript on Freenode.
RegExp.multilineproperty, but I quickly found out that it only changes the meaning of^and$.David Lindquist - 20th September 2004 23:35 - #
Not to say *tisk-tisk* or anything Simon, but this is identical to how PHP handles regular expressions by default. ;)
But I do sympathize with how frustrating this behaviour can be, especially when dealing with possibly corrupted data. For example, whenever I parse XML through PHP I now force myself to include
preg_replace( "/>\s*</", '><', $xml_str );before any parsing takes place. These facts of regular expressions are just painful concepts that must be bludgeoned into our heads.Stephen - 23rd September 2004 22:47 - #
Udo Borkowski - 13th December 2005 08:08 - #
Tino Zijdel - 23rd January 2006 22:19 - #
Suresh V - 5th May 2006 11:21 - #