0

I am working on an autocomplete used inside a textarea. I know there is some autocompletes already created, but anyway.

It works well, but if when I'm typing something and I select one or many characters and delete it, a  appears at the end of my string (or where I was inside it). I tried to replace it while retrieving my html with replaceAll, but it doesn't work (There is not this special char when I use an indexOf). The problem is he doesn't find any result because of this char. Let's see an exemple :

This is my array (a little bit cut but we don't really care)

let array = [{
    name: "test",
    value: "I'm a test value"
},
{
    name: "valueorange",
    value: "I'm just an orange"
},

// This is how I get the contents of my span (I tried both innerHTML and innerText, same results). 
// Same while using .text() or .html() with jquery

let value = jqElement.find("#searching-span")[0].innerHTML.substring(1).toLowerCase();
    
value = value.replaceAll(" ", " ");
value = value.replaceAll("", "");

I can replace every   without any problems. Finally I check with a loop if there is some value with indexOf on each value, and if it returns anything I push it and get it in a new array. But when I have  I have no results.

Any idea how I can resolve it ?

I tried to be clear, I hope my english wasn't so bad, sorry if I made many mistakes !

Drallireeh
  • 17
  • 6
  • Look here for possible answers: https://stackoverflow.com/questions/6784799/what-is-this-char-65279 –  Oct 27 '20 at 09:03

1 Answers1

1

Character entities and HTML escaped characters like   and  appearing in HTML source code are converted by the HTML parser into unicode characters like \u00a0 and \ufeff before being inserted into the DOM.

If replacing them in JavaScript, use their unicode characters, not HTML escape sequences, to match them in DOM strings. For example:

p.textContent = p.textContent.replaceAll("\ufeff", '*'); // zwj
p.textContent = p.textContent.replaceAll("\xa0", '-');   // nbsp
<p id="p">&#65279;&nbsp;&#65279;&nbsp;&#65279;&nbsp;</p>

Note that zero width joiners are uses a lot in emoji character sequences and arbitrarily removing may break emoji character decoding (although decoding badly formed emoji strings is almost a prerequisite for handling emojis in the wild).

Second note: I am not suggesting this as a means of circumventing badly decoding characters that have been encoded using a Unicode Transform Format. Making sure decoding is performed correctly is always a better option.

traktor
  • 17,588
  • 4
  • 32
  • 53
  • Incredible answer, it works perfectly for me ! Thanks you very much ! Was searching a solution since yesterday and you saved me ! – Drallireeh Oct 27 '20 at 09:54