Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript match and replace with unicode

I have some javascript which reads some cookies, and does some matching. I am using groupings to catch parts of a regular expression and use them later. I am having an issue with unicode characters though. If I have a character like \u25BA ►, when I find this character in a grouping, it returns the string '\u25BA' instead of the unicode character I am looking for. If I specify the character, I can fix the problem, but I can't get it to work more generally. The following will work as I want it to:

var matches=theOnclick.match(/.*\(event, ?"([^"]*)", ?"([^"]*)".*\)/);
var expand=matches[1].replace(/\\u25BA/, '\u25BA');

but this will not work:

var expand=matches[1].replace(/\\u([0-9A-Z])/, '\u\1');

any suggestions?

More info: Thanks for your answers. Let me add a little more background. I think the problem is is that I am getting my matches from an "onclick" on a span. I've included a slightly more detailed example below. If I have a normal string with unicode characters in it, when I do a match, I get the unicode characters. However, when I grab the string from the value of the onclick, I get the unicode escape sequences instead. So I have been trying to convert the unicode escape sequences back into the unicode characters. I hope that makes sense. Perhaps there is another way to do it.

In the example below, bar behaves as I want it to, and foo does not.

<html>
<body>
<span id='foo' onclick='expandCollapse(event, "►", "▼");'>foo</span>
<script type='text/javascript'>
var foo= document.getElementById('foo').onclick+'';
alert(foo);
var foomatches=foo.match(/.*\(event, ?"([^"]*)", ?"([^"]*)".*\)/);
alert(foomatches);
var bar='expandCollapse(event, "►", "▼");'
var barmatches=bar.match(/.*\(event, ?"([^"]*)", ?"([^"]*)".*\)/);
alert(barmatches);
</script>
</body>
</html>
like image 541
Robert Felty Avatar asked Oct 14 '22 15:10

Robert Felty


1 Answers

I think your \1 is wrong. It should be like:

"\u2603".replace(/(.)/, "$1"))

$1 is a back-reference in the replacement text.

But I'm not entirely sure what you're trying to do. Keep in mind \u can only be used with unicode literals. So the \u in the regex matches a literal \ then a literal u. And the \u in the replacement text is a u.

like image 195
Matthew Flaschen Avatar answered Oct 20 '22 15:10

Matthew Flaschen