1

In a React app, I need to send to a backend a search term with Unicode Decimal characters.

If the user enters ä, I need to convert it to ä

If the user enters KivisiltaöøäÅåÄ I need to convert it to KivisiltaöøäÅåÄ

How to do it automatically for all most common latin letters and Latin-1 supplement?

I found this very useful article to parse it: Replace unicode characters with characters (Javascript) But I need the contrary, to encode it.

1 Answer 1

2

If you want to encode every non-ASCII character, use this:

function encode(str) {
  let out = "";
  for (const char of str) {
    const code = char.codePointAt(0);
    if (code >= 0x80)
      out += `&#${code};`;
    else
      out += char;
  }
  return out;
}

console.log(encode("KivisiltaöøäÅåÄ"));

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

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.