我有一个字符串,我想用空字符串替换某些值。我的字符串是:

"col1=,,,&col2=,&col3=,,&item5,item8,"


我正在尝试将“,”替换为“”

我尝试了以下内容,但并不高兴,有人对实现此目标的最佳方法有任何想法吗?

var string = "col1=,,,&col2=,&col3=,,&item5,item8,";
var NewCookie = "";

NewCookie = string.replace(",,", ",");
NewCookie = NewCookie.replace(',,,', ',');
NewCookie = NewCookie.replace(',,&', ',');

alert(NewCookie);


谢谢

最佳答案

试试这个功能:

function RemoveConsecutiveCharacter(str, character){
    var newStr = "";

    $.each(str, function(index){
        if(str[index] != character || str[index-1] != character){
            newStr += str[index];
        }
    });

    return newStr;
}


在此处查看示例:http://jsfiddle.net/expertCode/6naAB/

关于javascript - JavaScript字符串替换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7359938/

10-11 04:13