替换所有javascript

替换所有javascript,javascript,replace,Javascript,Replace,我有这个问题 我想用一个ecc来取代所有像“ecc”这样的角色 我有一个可以工作的原型: String.prototype.ReplaceAll = function(stringToFind,stringToReplace){ var temp = this; var index = temp.indexOf(stringToFind); while(index != -1){ temp = temp.replace(stringToFi

我有这个问题 我想用一个ecc来取代所有像“ecc”这样的角色

我有一个可以工作的原型:

String.prototype.ReplaceAll = function(stringToFind,stringToReplace){
    var temp = this;
    var index = temp.indexOf(stringToFind);
        while(index != -1){
            temp = temp.replace(stringToFind,stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
    };
现在我想将这个原型与我的函数Clean一起使用:

function Clean(temp){
temp.ReplaceAll("è","è");
temp.ReplaceAll("à","à");
temp.ReplaceAll("ì","ì");
temp.ReplaceAll("ò","ò");
temp.ReplaceAll("ù","ù");
temp.ReplaceAll("é","&eacuta;");
return temp;
}
现在我想这样使用我的函数:

var name= document.getElementById("name").value;
var nomePul=Clean(name);
function Clean(temp){
    temp = temp.ReplaceAll("è","è");
    temp = temp.ReplaceAll("à","à");
    temp = temp.ReplaceAll("ì","ì");
    temp = temp.ReplaceAll("ò","ò");
    temp = temp.ReplaceAll("ù","ù");
    temp = temp.ReplaceAll("é","&eacuta;");
    return temp;
}
为什么这不起作用?怎么了

在这种情况下,它可以工作(没有我的函数clean,所以我认为问题就在那里)

有人能帮我吗?

使用以下方法:

function Clean(temp){
temp=temp.ReplaceAll("è","è");
temp=temp.ReplaceAll("à","à");
temp=temp.ReplaceAll("ì","ì");
temp=temp.ReplaceAll("ò","ò");
temp=temp.ReplaceAll("ù","ù");
temp=temp.ReplaceAll("é","&eacuta;");
return temp;
}
您没有指定值

ReplaceAll()返回字符串。所以你应该这么做

temp = temp.ReplaceAll("è","è");

在Clean()函数中,
ReplaceAll
函数不会改变字符串。它返回一个新字符串。这意味着您需要分配它,如下所示:

var name= document.getElementById("name").value;
var nomePul=Clean(name);
function Clean(temp){
    temp = temp.ReplaceAll("è","è");
    temp = temp.ReplaceAll("à","à");
    temp = temp.ReplaceAll("ì","ì");
    temp = temp.ReplaceAll("ò","ò");
    temp = temp.ReplaceAll("ù","ù");
    temp = temp.ReplaceAll("é","&eacuta;");
    return temp;
}
请注意,原型方法可以链接,因此如果您这样做,重复性可能会稍微减少:

function Clean(temp){
    return temp.ReplaceAll("è","è")
        .ReplaceAll("à","à")
        .ReplaceAll("ì","ì")
        .ReplaceAll("ò","ò")
        .ReplaceAll("ù","ù")
        .ReplaceAll("é","&eacuta;");
}
如果愿意,可以使用Javascript中执行全局替换的典型方式,这样就不需要使用定制的
ReplaceAll
prototype函数

    return temp.replace(/è/g,"è")
        .replace(/à/g,"à")
        .replace(/ì/g,"ì")
        .replace(/ò/g,"ò")
        .replace(/ù/g,"ù")
        .replace(/é/g,"&eacuta;");

这里是replaceAll的另一个实现。希望它能帮助别人

    String.prototype.replaceAll = function (stringToFind, stringToReplace) {
        if (stringToFind === stringToReplace) return this;
        var temp = this;
        var index = temp.indexOf(stringToFind);
        while (index != -1) {
            temp = temp.replace(stringToFind, stringToReplace);
            index = temp.indexOf(stringToFind);
        }
        return temp;
    };

在javascript中可以通过两种方式应用此功能

1) 使用替换的字符串拆分并连接字符串数组:

  return temp.split("è").join("è")
        .split("à").join("à")
        .split("ì").join("ì")
        .split("ò").join("ò")
        .split("ù").join("ù")
        .split("é").join("&eacuta;");
2) 使用内置于javascript本身的全局替换(如上所述)


尝试以下代码替换所有。您可以使用此代码生成函数

var str = "Test abc test test abc test test test abc test test abc";
str = str.split('abc').join('');
alert(str);

而不是
temp.ReplaceAll(“è”和“è;”)
,替换Javascript中字符串的所有实例的典型方法是使用正则表达式:
temp.replace(/è/g,“è;”)
.也不能这样工作:(它可以,但你需要使用赋值。现在你只是在丢弃返回函数的结果。是的!!!!!谢谢!!!我给你正确的答案,因为你是第一个:)