Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace unicode space characters

I need to replace the unicode characters defined on here

I have got this so far but it seems to remove all space including standard spacebar ones:

var str = "Hello this is a test  of the site"; 
str= str.replace(/[ \u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,'')

Result - Hellothisisatestofthesite

I only want to remove the unicode character which is U+2003 between 'test' and 'of' in the string.

like image 837
user2025749 Avatar asked May 07 '13 10:05

user2025749


People also ask

How do you replace letters with spaces?

Use the replaceAll() method to replace a character with a space. The method takes the character to be replaced and the replacement string as parameters, e.g. str. replaceAll('_', ' ') . The method returns a new string with all occurrences of the character replaced by the provided replacement.

How do you put a space in Unicode?

If you want to use space, just assign space = u" " . Try to avoid mixing byte strings and Unicode, use u" ".


2 Answers

Remove the regular space that you have first in the pattern:

 str = str.replace(/[\u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,'');
like image 165
Guffa Avatar answered Sep 25 '22 13:09

Guffa


try this:

var str = "Hello this is a test  of the site";
str= str.replace(/[\u00A0\u1680​\u180e\u2000-\u2009\u200a​\u200b​\u202f\u205f​\u3000]/g,'')

same as you did, but with out ' ' (regular space)

like image 38
Kuf Avatar answered Sep 24 '22 13:09

Kuf