Javascript substrings multiline replace by RegExp

Posted by Radek Šimko on Stack Overflow See other posts from Stack Overflow or by Radek Šimko
Published on 2010-12-26T10:45:19Z Indexed on 2010/12/26 10:54 UTC
Read the original article Hit count: 277

Filed under:
|

Hi, I'm having some troubles with matching a regular expression in multi-line string.

<script>
var str="Welcome to Google!\n";
str = str + "We are proud to announce that Microsoft has \n";
str = str + "one of the worst Web Developers sites in the world.";

document.write(str.replace(/.*(microsoft).*/gmi, "$1"));
</script>

http://jsbin.com/osoli3/3/edit

As you may see on the link above, the output of the code looks like this:

Welcome to Google! Microsoft one of the worst Web Developers sites in the world.

Which means, that the replace() method goes line by line and if there's no match in that line, it returns just the whole line... Even if it has the "m" (multiline) modifier...

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about regex