2

I have this problem I'm triyng to replace all the charachters like àòùèì with à ecc...

I have this prototype that works:

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;
    };

Now I want to use this prototype with my function called Clean:

function Clean(temp){
temp.ReplaceAll("è","è");
temp.ReplaceAll("à","à");
temp.ReplaceAll("ì","ì");
temp.ReplaceAll("ò","ò");
temp.ReplaceAll("ù","ù");
temp.ReplaceAll("é","&eacuta;");
return temp;
}

and now I want to use my function like this:

var name= document.getElementById("name").value;
var nomePul=Clean(name);

Why this does not work? What is wrong?

In this case it works (without my function clean, so I think the problem is there)

var nomePul=nome.ReplaceAll("è","è");

Someone can help me?

3
  • 1
    Instead of temp.ReplaceAll("è", "è");, the typical way to replace all instances of a string in Javascript is to use a regex: temp.replace(/è/g, "è");. Commented Nov 7, 2012 at 19:04
  • does not work either in this way:( Commented Nov 7, 2012 at 19:07
  • It does, but you need to use an assignment. Right now you're just throwing away the result of the returned function. Commented Nov 7, 2012 at 19:11

6 Answers 6

3

Use the following:

function Clean(temp){
temp=temp.ReplaceAll("è","è");
temp=temp.ReplaceAll("à","à");
temp=temp.ReplaceAll("ì","ì");
temp=temp.ReplaceAll("ò","ò");
temp=temp.ReplaceAll("ù","ù");
temp=temp.ReplaceAll("é","&eacuta;");
return temp;
}

You are not assigning the value

Sign up to request clarification or add additional context in comments.

1 Comment

yes!!!!!! thanks!!! I give to you the correct answer because you was the first :)
2

Here's another implementation of replaceAll. Hope it helps someone.

    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;
    };

Comments

1

ReplaceAll() returns a string. So you should do

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

in your Clean() function

Comments

1

The ReplaceAll function does not mutate the string. It returns a new string. That means you need to assign it, like this:

function Clean(temp){
    temp = temp.ReplaceAll("è","è");
    temp = temp.ReplaceAll("à","à");
    temp = temp.ReplaceAll("ì","ì");
    temp = temp.ReplaceAll("ò","ò");
    temp = temp.ReplaceAll("ù","ù");
    temp = temp.ReplaceAll("é","&eacuta;");
    return temp;
}

Note that prototype methods can be chained, so you could be slightly less repetitive if you did this:

function Clean(temp){
    return temp.ReplaceAll("è","è")
        .ReplaceAll("à","à")
        .ReplaceAll("ì","ì")
        .ReplaceAll("ò","ò")
        .ReplaceAll("ù","ù")
        .ReplaceAll("é","&eacuta;");
}

And if you like, you can use the typical way global replaces are done in Javascript instead, so then you don't need to use the custom ReplaceAll prototype function.

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

Comments

0

you could apply this in 2 ways in javascript ..

1) Split and join strings array with your replaced string:

  return temp.split("è").join("è")
        .split("à").join("à")
        .split("ì").join("ì")
        .split("ò").join("ò")
        .split("ù").join("ù")
        .split("é").join("&eacuta;");

2) Using global replace that is built in javascript itself (As Peter specified above)

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

Comments

0

Try following code to replace all. You can make function with this code.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.