admin管理员组文章数量:1426894
A website I'm modding with a userscript has some text I want to modify. The text appears to have a unicode character in it. When I look at it on screen or even extract it to a variable with jQuery, it looks like this:
2 others
However, if I create my own variable with that same text and then do a parison, they e up as false. So I copied/pasted the site's text into vim and it looks like this:
2<200e> others
Best I can tell this is a unicode character for space (?). I want to be able to match this string with a regex such as:
^(\d+(?:,\d+)*)\s+(.*)
but on this string with the embedded unicode character it fails. (it works fine on my own typed text of '2 others').
Is there some way I can strip this unicode out of the text? I tried the following, to no avail:
text.replace('\u200e\','')
text.replace('200e','')
text.replace('\%20','')
text.replace('\%u200e','')
Or, alternatively, can I adjust my regex to match either '2 others' or the same text with the embedded 200e unicode char?
A website I'm modding with a userscript has some text I want to modify. The text appears to have a unicode character in it. When I look at it on screen or even extract it to a variable with jQuery, it looks like this:
2 others
However, if I create my own variable with that same text and then do a parison, they e up as false. So I copied/pasted the site's text into vim and it looks like this:
2<200e> others
Best I can tell this is a unicode character for space (?). I want to be able to match this string with a regex such as:
^(\d+(?:,\d+)*)\s+(.*)
but on this string with the embedded unicode character it fails. (it works fine on my own typed text of '2 others').
Is there some way I can strip this unicode out of the text? I tried the following, to no avail:
text.replace('\u200e\','')
text.replace('200e','')
text.replace('\%20','')
text.replace('\%u200e','')
Or, alternatively, can I adjust my regex to match either '2 others' or the same text with the embedded 200e unicode char?
Share Improve this question asked Oct 3, 2012 at 8:05 mixmix 7,16115 gold badges65 silver badges94 bronze badges1 Answer
Reset to default 6Try to use an actual regex instead.
text = text.replace(/\u200e/g, '');
can I adjust my regex to match either '2 others' or the same text with the embedded 200e unicode char?
You could just change the \s
in your regex to include U+200E as well, e.g.
^(\d+(?:,\d+)*)[\s\u200e]+(.*)
本文标签: how to strip (or regex match) a unicode character from a string in javascriptStack Overflow
版权声明:本文标题:how to strip (or regex match) a unicode character from a string in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745486379a2660402.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论