Martina
Martina

Reputation: 1918

replaceAll javascript

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?

Upvotes: 2

Views: 7157

Answers (6)

Fakhrul Hasan
Fakhrul Hasan

Reputation: 162

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

Upvotes: 0

Muhammad Soliman
Muhammad Soliman

Reputation: 23756

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;");

Upvotes: 0

scripto
scripto

Reputation: 2297

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

Upvotes: 2

Peter Olson
Peter Olson

Reputation: 142921

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;");

Upvotes: 1

Mihai
Mihai

Reputation: 2760

ReplaceAll() returns a string. So you should do

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

in your Clean() function

Upvotes: 1

Th3Nic3Guy
Th3Nic3Guy

Reputation: 1979

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

Upvotes: 3

Related Questions