replace
"javascript中替换字符串用哪个函数?".replace(/a/g, "A");

解决方案 »

  1.   

    METHOD:  String::replace --------------------------------------------------------------------------------
    object.replace(regexp, newSubStr)
    object.replace(regexp, function)This method is used to match a specifed regular expression against a string and replace any match with a new substring. The newSubStr argument can include certain RegExp properties. These are: $1 thru $9, lastMatch, lastParen, leftContext and rightContext (for details on the RegExp object's properties, go here). To perform a global match include the 'g' (global) flag in the regular expression and to perform a case-insensitive match include the 'i' (ignore case) flag.The second argument can also be a function which is invoked after the match is performed and the result of which is used as the replacement string. The following code uses the replace method to alter 'DevGuru' in the original string to the full URL for the DevGuru website and then uses the String.link method to provide a hyperlink to the site.Code:
    myString = new String("Go to DevGuru today!")
    rExp = /devguru/gi;
    newString = new String ("http://www.devguru.com")
    results = myString.replace(rExp, newString.link("http://www.devguru.com"))
    document.write(results)Output:
    Go to http://www.devguru.com today! 另推荐一个自创的写正则的工具:
    http://blog.csdn.net/zhaoxiaoyang
      

  2.   

    要是替换一个变量怎么办呢?要全局替换的,不要只替换一个
    var dd="01";
    var cc="0102010201020102";cc.replace(dd,"y");
    好象不行啊,只能替换一个,不能全局替换
      

  3.   

    cc.replace(new RegExp(dd, "g"), "y");