最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

regex - How do I replace unicode character u00A0 with a space in javascript? - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 12

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.

发布评论

评论列表(0)

  1. 暂无评论