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

string - JavaScript: replace non breaking space and special space characters with ordinary space - Stack Overflow

programmeradmin3浏览0评论

I was trying to debug a problem of searching inside a string and it came down to the following interesting piece of code.

Both "item " and "item " seem equal but they are not!

var result = ("item " === "item ");

document.write(result);
console.log(result);

I was trying to debug a problem of searching inside a string and it came down to the following interesting piece of code.

Both "item " and "item " seem equal but they are not!

var result = ("item " === "item ");

document.write(result);
console.log(result);

After investigating this further by pasting it on a Python interpreter, I found out that the first "item " has a different kind of space as "item\xc2\xa0". Which I think is a non breaking space.

Now, A possible solution to match these strings will be to replace \xc2\xa0 with space, but is there a better approach to convert all special space characters with normal space?.

Share Improve this question edited Jan 13, 2020 at 20:42 Irshad P I asked Jan 9, 2020 at 7:40 Irshad P IIrshad P I 2,8543 gold badges16 silver badges24 bronze badges 5
  • Check "item ".charCodeAt(4) of both strings. They are different – adiga Commented Jan 9, 2020 at 7:47
  • Yes, I have done that, and the characters are different as mentioned in the question. My question is on how to approach with paring these strings? (without trimming/removing spaces) – Irshad P I Commented Jan 9, 2020 at 7:54
  • I was just mentioning how to identity it without using python interpreter. – adiga Commented Jan 9, 2020 at 7:57
  • You might want to take a look at the possible space characters in Unicode here: unicode-table./en/search/?q=space there are also some language specific space characters. So this can get really tricky if you are perfectionistic. – Krisztián Balla Commented Jan 9, 2020 at 8:10
  • @JennyO'Reilly, Haha, thank you. I will definitely check that out. FYI, my perfectionistic parameter is at 90 percent. – Irshad P I Commented Jan 9, 2020 at 10:03
Add a ment  | 

3 Answers 3

Reset to default 9

In ES2015/ES6 you can use the String.Prototype.normalize() method to depose both characters to the same simple space character:

const normalize = str => str.normalize('NFKD');
console.log(normalize("item\u0020") === normalize("item\u00a0"));

The space in the first string is character code 160 (a non-breaking space), and the space in the second string is character code 32 (a normal space), so the strings aren't equal to each other.

console.log("item ".charCodeAt(4), "item ".charCodeAt(4));

is there a better approach to convert all special space characters with normal space?.

You can match space characters which aren't tabs or newlines and replace with a normal space:

const makeSpacesNormal = str => str.replace(/(?=\s)[^\r\n\t]/g, ' ');
console.log(makeSpacesNormal("item ") === makeSpacesNormal("item "));

Specifically, the \s will match a whole bunch of space-like characters:

[\t\n\v\f\r \u00a0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000]

and by matching and replacing those (except newlines and tabs, if you want), you'll be left with ordinary spaces.

trim will remove all whitespace from the beginning and end of a string. If you want to pare two strings while ignoring leading and trailing whitespace, trim both of them.

"item ".trim() === "item ".trim()
发布评论

评论列表(0)

  1. 暂无评论