admin管理员组文章数量:1292753
I have a string with the UTF-8 character ↵
. To my understanding, if you want to replace a UTF-8 character in a string, you specify the character with its hexadecimal representation, like so:
var string = "↵↵↵Middle↵↵↵";
console.log("Match? " + /\u21b5/.test("↵"));
console.log(string);
string = string.replace("/\u21b5/g", "");
console.log(string);
It is a match, but the replace is not working. What am I missing?
JSFiddle
I have a string with the UTF-8 character ↵
. To my understanding, if you want to replace a UTF-8 character in a string, you specify the character with its hexadecimal representation, like so:
var string = "↵↵↵Middle↵↵↵";
console.log("Match? " + /\u21b5/.test("↵"));
console.log(string);
string = string.replace("/\u21b5/g", "");
console.log(string);
It is a match, but the replace is not working. What am I missing?
JSFiddle
Share Improve this question edited Aug 4, 2016 at 14:00 Tholle asked Mar 13, 2015 at 10:32 TholleTholle 113k22 gold badges208 silver badges197 bronze badges 1-
1
FYI: That's a unicode character (more precisely, a unicode code point), UTF-8 is just one possible encoding. If UTF-8 was used, it would be represented by the bytes
E2 86 B5
, but JavaScript uses USC-2 where this character is the 16 bit word21B5
. – user395760 Commented Mar 13, 2015 at 10:42
2 Answers
Reset to default 10You are using a string not a regex
string = string.replace(/\u21b5/g, "");
replace
string = string.replace("/\u21b5/", "");
with
string = string.replace(/\u21b5/g, "");
本文标签: javascriptCan39t replace UTF8 character with RegExpStack Overflow
版权声明:本文标题:javascript - Can't replace UTF-8 character with RegExp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741561853a2385493.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论