views:

400

answers:

3

I'm trying to replace something multi line in Javascript, IE is giving me trouble.

pastebin here:http://jsbin.com/olahi/edit

Explanation of what I'm trying to do: Here's my HTML:

<textarea id="editthis">
Hello from JS Bin
Whats up from JS Bin
Whats
up from JS Bin
</textarea >

And my JS:

jQuery.fn.runReplacement= function(expr) {
    return this.each(function() {
     this.innerHTML = this.innerHTML
     .replace(/Hello/ig, "Hey")
     .replace(/Whats ?\n? ?up/ig, "Hey")
     ;
    });
};

$(function() {
  $("textarea").runReplacement();
});

In my JS(jQuery) example, I'm just trying to replace Hello and 'Whats up' with 'Hey'.

In IE the first two lines work, but not the third(and fourth). In Chrome and FF all 4 lines work.

I don't see any reason why IE should not support \n. Did I do something wrong?

Thanks in advance.

Thanks everyone, it's good to learn that IE needs /r/n rather than just /n.

Any suggestions for making my code cross-browser, rather than just IE or everything else.

Thanks!

+1  A: 

It's actually working fine for me in IE6. This, however, should be a "better" regex as it'll match both \r and \n characters (which is where IE might be slipping up - seeing \r instead of, or as well as, \n), and any spaces.

str.replace(/Whats\s+up/ig, "Hey")
jwpage
Sorry, I need to match a new line specifically, not just a space.
Jourkey
For the record, in a regex \s will match any "whitespace" - this includes spaces (' '), tabs (\t), carriage returns (\r), newlines (\n).
jwpage
A: 

CR/LF characters are slightly differs from IE to other browsers. In IE it is \r\n.

cross browser solution: /Whats ?\r?\n? ?up/

Eldar Djafarov
Thanks, any suggestions for how to make it so that it works cross-browser?
Jourkey
see updated answer
Eldar Djafarov
+1  A: 

Internet Explorer uses \r\n as the newline character in a <textarea>.

Seph