Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string.replace("é", "e") not working

I have function that is supposed to "clean" a string and i'd like to use replace() to do that, but I can't figure out why the following code is not working when the text comes from an input[text].

for instance :

console.log(getCleanText("ééé")); // works fine, it displays : eee

but

// my_id is an input with type="text"
var my_text = document.getElementById("my_id").value 
console.log(getCleanText(my_text)); // doesn't work at all, it displays : ééé

the function code is :

function getCleanText(some_text) {
    var clean_text = some_text.toLowerCase();
    clean_text = clean_text.replace("é", "e"); 
    clean_text = clean_text.split("é").join("e"); // give it another try

    return clean_text;
}

any idea ?

like image 968
Banibal Avatar asked May 08 '26 01:05

Banibal


1 Answers

I'm willing to bet your problem lies in a misunderstanding of Unicode.

é 
é

Those two characters above are two different characters. The first is the letter e, with an accent character (U+0301). The other is a single character, U+00E9.

You need to ensure you're replacing both versions.

like image 143
Matt Grande Avatar answered May 09 '26 16:05

Matt Grande



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!