Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  29] [ 7]  / answers: 1 / hits: 19990  / 9 Years ago, fri, april 24, 2015, 12:00:00

I'm looking to replace a bad character that a content editable div uses when a space is added at the end of the input.



Here is what I've tried



var text = $(this).text();
text.replace(/u00A0/, );


But when I check the value of the last character like this



text.charCodeAt(text.length-1)


The value is still 160 instead of 32


More From » regex

 Answers
14

In javascript strings are immutable and replace return new string, try this:


var text = $(this).text();
text = text.replace(/u00A0/, " ");

or in one line:


var text = $(this).text().replace(/u00A0/, " ");

also if you want to replace all instances of the character, you need to add g flag to the regex /u00A0/g.


[#66909] Thursday, April 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neilshamarh

Total Points: 181
Total Questions: 94
Total Answers: 104

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
;