Replace all commas in a string

This question already has an answer here:

  • How to replace all occurrences of a string in JavaScript? 41 answers

  • You can use RegExp with g flag.

    var mystring = "this,is,a,test"
    mystring.replace(/,/g , "newchar");
    

    DEMO: http://jsfiddle.net/d4N9s/


    只是为了好玩:

    var mystring = "this,is,a,test"  
    var newchar = '|'
    mystring = mystring.split(',').join(newchar);
    

    var mystring = "this,is,a,test"
    mystring.replace(/,/g, "newchar");
    

    Use the global( g ) flag

    Simple DEMO

    链接地址: http://www.djcxy.com/p/16832.html

    上一篇: 通过JavaScript删除字符串中的分号

    下一篇: 替换字符串中的所有逗号