Javascript substrings multiline replace by RegExp
- by Radek Šimko
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...