0

I need some help with replacing multiple lines in a javascript. I have read something about using flags, but I dont know how to implement them.

This is my current replace()

var res = str.replace(selectedText,'<b>' + selectedText + '</b>');

It is working for a single line, but not for multiple lines.

If selectedText was:

aaa bbb

ccc

The result would be:

aaa bbb

ccc

Any tips?

//Ambrose

Ambrose
  • 163
  • 1
  • 1
  • 8
  • 1
    You should elaborate a bit more on what you want. Also adding more code could be helpful, so we can more accurately help you. – kemicofa ghost May 30 '14 at 08:52

1 Answers1

0

If you give replace a string for the first argument, only the first match is replaced. To replace all matches, you have to pass in an regular expression as the first argument, and use the g flag on it. This replaces all as with bs:

var str = "a a a a";
str = str.replace(/a/g, 'b');
console.log(str); // "b b b b"

So you'll need to create a regular expression for selectedText. You can do that via new RegExp(selectedText, "g"):

var res = str.replace(new RegExp(selectedText, "g"),'<b>' + selectedText + '</b>');

...but if selectedText has any characters in it that have special meaning in a regular expression, you have to escape those. Sadly, JavaScript has no built-in function for doing that, but if you search around you can find several examples of functions to do it. This answer on SO has some options as well.

If we use the RegExp.escape from this answer to that question (it's a bit overkill, but fairly harmless in its overkill-ness):

RegExp.escape= function(s) {
    return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
};

...then:

var res = str.replace(new RegExp(RegExp.escape(selectedText), "g"),'<b>' + selectedText + '</b>');
Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • It should only replace the first match shouldnt it? As I put in selectedText and basically want back my entire text except that the selectedText now is bold. – Ambrose May 30 '14 at 09:02
  • @Ambrose: Ah, I misunderstood. There's nothing special about line breaks. If you're not seeing the text get replaced, then the text isn't exactly what you think it is. For instance, if your text is `"aaaa bbbb\ncccc"` but the string you're replacing that in is `"aaaa bbbb
    cccc"` or even `"aaaa bbbb

    cccc

    "`, then of course it won't match and won't update. The flags you've heard about only affect the meaning of the special `^` and `$` markers in regular expressions; they wouldn't be relevant here.
    – T.J. Crowder May 30 '14 at 09:06
  • Hmm ok, that makes sense. But should that really matter as I am basically replacing selectedText with selectedText + minor additions? I am not trying to replace "aaa bbb\nccc"(=selectedText) with "aaa bbb

    ccc". I am replacing it with " aaa bbb\nccc (=selectedText) ". Am I not?

    – Ambrose May 30 '14 at 09:15
  • @Ambrose: Right. What I'm saying is that if you're not seeing a change, then `selectedText` isn't in the string. I was giving examples of how you might have `"aaaa bbbb\ncccc"` but the text in the string you're replacing is actually `"aaaa bbbb
    cccc"` and so it doesn't match and doesn't get updated.
    – T.J. Crowder May 30 '14 at 09:22