Skip to main content
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Ben Alman
Ben Nadel at the jQuery Conference 2010 (Boston, MA) with: Ben Alman ( @cowboy )

Javascript Regular Expression (RegExp) Multiline Flag Not Universal

Published in

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:

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

And should be replaced with:

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.

Want to use code from this post? Check out the license.

Reader Comments

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel