简体   繁体   中英

Do all browsers handle searching multi-line with regex?

Will all browsers (IE6+, FF3+, Safari 3+, Chrome) execute a javascript regex search across line boundaries in the searched string? Example:

var sourceStr = "This is some text \nOn multiple lines\nAnd the 2nd line.";
sourceStr = sourceStr.replace(/line/g, "xxx");

Also, are there any good references of what regex features are or aren't supported in various browsers.

Yes, but if you use any char syntax - dot "." in regex patterns, you may need to change it to [\s\S] to match any chars across lines.

The ECMA specification ( ECMA-262 ) dictates acceptable regular expression grammar and multiline support is explicitly mentioned. Section 15.10.2.6 in particular demonstrates the effect of multiline support on Assertions.

So, if the browser supports the ECMA-262 specification, then yes, it supports multiline.

Of course, you should endeavor to test your code before you can be confident it works. It wouldn't be the first time a browser has failed to implement a specification correctly.

EDIT: To clarify, JavaScript 1.5 is fully compatible with ECMA-262, Edition 3. I understand the following browsers support JavaScript version 1.5 or higher:

  • Safari 2.0 or newer (Mac)
  • Camino 0.8 or newer (Mac)
  • Firefox 0.9 or newer (Windows, Mac, Linux)
  • Internet Explorer 6.0 or newer (Windows)
  • Mozilla 1.2 or newer (Linux)
  • Netscape 7.1 or newer (Windows, Mac)
  • Opera 8.0 or newer (Windows, Mac, Linux)

Can't actually find a decent source for Chrome although I'd be shocked if it didn't support JavaScript >= 1.5.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM