65

Possible Duplicate:
Fastest method to replace all instances of a character in a string

How can you replace all occurrences found in a string?

If you want to replace all the newline characters (\n) in a string..

This will only replace the first occurrence of newline

str.replace(/\\n/, '<br />');

I cant figure out how to do the trick?

Community
  • 1
  • 1
clarkk
  • 27,151
  • 72
  • 200
  • 340

3 Answers3

125

Use the global flag.

str.replace(/\n/g, '<br />');
Brigham
  • 14,395
  • 3
  • 38
  • 48
  • 1
    https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace#Example.3a_Using_global_and_ignore_with_replace "Non-standard A string specifying a combination of regular expression flags. The use of the flags parameter in the String.replace method is non-standard. Instead of using this parameter, use a RegExp object with the corresponding flags." – Beta033 May 07 '13 at 22:05
  • 2
    What's your point? My solution doesn't use the non-standard flags parameter. – Brigham May 07 '13 at 22:16
  • Thanks Brigham. Your code works very well... – csonuryilmaz May 08 '13 at 15:00
  • 2
    I don't think this is a universal solution as your function seems to be incapable, of replacing "|"s with "~~" or something like that `var text= "|ABC|DEF||XYZ|||"; text = replaceAllSubString(text, '|', '~~'); alert(text);` and a function defN : `function replaceAllSubString(targetString, subString, replaceWith) { while (targetString.indexOf(subString) != -1) { targetString = targetString.replace(subString, replaceWith); } return targetString; }` [jsbin](http://jsbin.com/ivenay/1/) – ablaze Aug 06 '13 at 17:23
  • 3
    `|` is a special character in regular expressions, so you must escape it: `text = text.replace(/\|/g, '~~')` – Brigham Aug 06 '13 at 18:12
  • I had a hard time with using this answer since my string had a bunch of special regex characters, I used jsbins solution and it works great and it is obvious. Note I couldn't get Matt's code to work(below). – Rob Aug 12 '14 at 15:25
41

Brighams answer uses literal regexp.

Solution with a Regex object.

var regex = new RegExp('\n', 'g');
text = text.replace(regex, '<br />');

TRY IT HERE : JSFiddle Working Example

Kerem Baydoğan
  • 10,475
  • 1
  • 43
  • 50
  • Uh, the *parameter* is non-standard. Using the flag directly after the expression is fine. You're essentially doing the same thing as the other answer except using the RegExp constructor (which is commonly used for dynamic expressions). –  May 19 '11 at 21:46
  • @Matt Says who? There is reference from Mozilla in my post. Where is your reference? – Kerem Baydoğan May 19 '11 at 21:47
  • You're misreading the source. `//` is the literal form of a RegExp object. MDC states the third (not the first) parameter is non-standard. –  May 19 '11 at 21:54
  • 1
    /\\n/ is a regex, '\\n' would be a string. – James May 19 '11 at 22:04
  • The very first example in your link uses flags on a regex literal, as in my answer. – Brigham May 19 '11 at 23:12
  • @Matt @Brigham yeah you are right. In this case literal usage seems to be okay. String.replace has some different implementations so i am always using regexp form to avoid incompatibility. – Kerem Baydoğan May 19 '11 at 23:35
  • Your sample does nothing - had it worked, you would have seen 2 `
    `s in one line instead of three lines. You shouldn't include the delimiters in the string `"/\\n/"` should be simply `"\n"`. You don't *have to* escape the backslash either, but that one will work either way.
    – Kobi May 20 '11 at 06:17
  • 1
    RegExp Second function is working!!! – ehsan Aug 12 '11 at 18:13
  • 1
    Great simple solution +1 – DeadlyChambers Jul 09 '14 at 15:57
  • This is a great answer, it also works with simple unicode matching like removing bom character using `new RegExp(String.fromCharCode(65279), 'g')` – Bill Yang Dec 08 '15 at 19:01
0

As explained here, you can use:

function replaceall(str,replace,with_this)
{
    var str_hasil ="";
    var temp;

    for(var i=0;i<str.length;i++) // not need to be equal. it causes the last change: undefined..
    {
        if (str[i] == replace)
        {
            temp = with_this;
        }
        else
        {
                temp = str[i];
        }

        str_hasil += temp;
    }

    return str_hasil;
}

... which you can then call using:

var str = "50.000.000";
alert(replaceall(str,'.',''));

The function will alert "50000000"

Matt
  • 74,352
  • 26
  • 153
  • 180
Dika Arta
  • 43
  • 1