Tholle Tholle - 4 months ago 36
Javascript Question

Can't replace UTF-8 character with RegExp

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

Answer

You are using a string not a regex

string = string.replace(/\u21b5/g, "");