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
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
Share Improve this question asked Apr 24, 2015 at 14:40 WryteWryte 8953 gold badges10 silver badges23 bronze badges 2-
Why not just cut the last character? Smth like this:
text.substr(0, -1)
– hindmost Commented Apr 24, 2015 at 14:45 - I need the last character since this text is a query parameter to a filter – Wryte Commented Apr 24, 2015 at 14:51
1 Answer
Reset to default 12In 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
.