Kosmetika
Kosmetika

Reputation: 21314

Replace all special characters like 'é' in URL with JavaScript

I'm receiving response from an API that contains name strings with special letters like 'é'.

Then I need to make a request to another API with query string containing this name with 'é'. API is third-party service which doesn't understand this letters.

encodeURIComponent doesn't help, server still returns an error.

I would like to replace special characters with existing alternatives like 'é'=>'e', is there a library or some ready solution for this purpose?

Upvotes: 1

Views: 2657

Answers (2)

Krish R
Krish R

Reputation: 22721

Did you tried the encodeURI

encodeURI - To encode an URL

encodeURIComponent - to encode the query string parameter

Upvotes: 0

Prinzhorn
Prinzhorn

Reputation: 22518

Those characters are called diacritic (more specifically this tiny stroke above the e).

Here's a JS lib https://github.com/superjoe30/diacritics

You need to modify it slightly in order to use it without any module loader.

Just replace exports.remove with sth. like window.removeDiacrits and then

str = removeDiacrits(str);

and it's probably a good idea to wrap the code in a IIFE.

Upvotes: 1

Related Questions