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?

0

3 Answers 3

125

Use the global flag.

str.replace(/\n/g, '<br />');
5
  • 1
    developer.mozilla.org/en-US/docs/JavaScript/Reference/… "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 7, 2013 at 22:05
  • 2
    What's your point? My solution doesn't use the non-standard flags parameter.
    – Brigham
    May 7, 2013 at 22:16
  • 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
    – ablaze
    Aug 6, 2013 at 17:23
  • 3
    | is a special character in regular expressions, so you must escape it: text = text.replace(/\|/g, '~~')
    – Brigham
    Aug 6, 2013 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, 2014 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

8
  • 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).
    – user1385191
    May 19, 2011 at 21:46
  • @Matt Says who? There is reference from Mozilla in my post. Where is your reference? May 19, 2011 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.
    – user1385191
    May 19, 2011 at 21:54
  • 1
    /\\n/ is a regex, '\\n' would be a string.
    – James
    May 19, 2011 at 22:04
  • The very first example in your link uses flags on a regex literal, as in my answer.
    – Brigham
    May 19, 2011 at 23:12
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"

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.