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 word 21B5. – user395760 Commented Mar 13, 2015 at 10:42
Add a ment  | 

2 Answers 2

Reset to default 10

You 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