Javascript Regular Expression (RegExp) Multiline Flag Not Universal

Posted May 11, 2006 at 8:18 AM

Tags: Javascript / DHTML

I was debugging some code and I just came accross a very interesting fact: the "m" flag for multiline searches in Regular Expression (RegExp) is not universal. It only works on newer browsers and not all of them for that matter. On this Javascript Site, it states that all searches work for multiline without the flag and that the "m" flag will indeed throw errors in older browsers (as I am finding in my debugging... stupid Mac IE).

So, to drive it home, this is not good:

 Launch code in new window » Download code as text file »

  • new RegExp("[\\n\\r]+", "gim")

And should be replaced with:

 Launch code in new window » Download code as text file »

  • new RegExp("[\\n\\r]+", "gi")

Both of these are searching for new line characters and returns across multiple lines; however, only the latter works in all browsers and is more "correct" for Javascript.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben

Recent Blog Comments
Mar 12, 2010 at 9:51 AM
FLEX On jQuery: Decouple Components With Event Listeners
@Tyson, Sounds awesome. I know very little about FLEX itself, other than these few interactions I've had with FLEX developers. As you start to learn stuff, I'd to hear how it influences your Javasc ... read »
Mar 12, 2010 at 9:44 AM
FLEX On jQuery: Decouple Components With Event Listeners
This is an excellent example of loose coupling. I've been meaning to learn more about Flex and I think seeing how you've applied it to your example may be just what I need to push me over the edge a ... read »
Mar 12, 2010 at 9:35 AM
Google Maps Not Working in Internet Explorer (IE)
@James Can you provide a link to your map please. Ralph ... read »
Mar 12, 2010 at 9:19 AM
Google Maps Not Working in Internet Explorer (IE)
I'm working on a project and am having a similar problem. The page "loads fine" in IE6/7 so long as I don't expect markers to load. Using jquery1.4, easing plugin, maps api 2, extinfowindow, and ma ... read »
Mar 12, 2010 at 7:30 AM
Ask Ben: Building An AJAX, jQuery, And ColdFusion Powered Application
@Nathan, In the function that returns the JSON, you can try placing output="false" as a parameter to the function and then use a cfsavecontent tag surrounding your desired output (you'll need cfoutp ... read »
Mar 12, 2010 at 6:17 AM
MySQL: The Multi-part Identifier "u.id" Could Not Be Bound
Yes that did help. Thanks ... read »
Mar 12, 2010 at 4:15 AM
Using Base64 Canvas Data In jQuery To Create ColdFusion Images
Just seen a very similar concept here: http://mrdoob.com/projects/harmony/ Which they say: "As it works on webkit, he made sure it worked on the mobile Android and iPhone browsers. No multi-touc ... read »
Mar 12, 2010 at 1:38 AM
Using jQuery's SlideUp() and SlideDown() Methods With Bottom-Positioned Elements
Very nice and useful tutorials for web designers, Thanks for posting. ... read »