Javascript compatibility and unicode replacement

I have javascript that reads some cookies and makes some matches. I use groupings to catch parts of a regular expression and use them later. However, I have a problem with unicode characters. If I have a character like \ u25BA ►, when I find this character in a group, it returns the string '\ u25BA' instead of the Unicode character I'm looking for. If I specify a symbol, I can fix the problem, but I cannot get it to work more generally. The following will work the way I want:

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?

Additional Information: Thank you for your answers. Let me add a little more background. I think the problem is that I get my matches from "onclick" on the fly. I gave a slightly more detailed example below. If I have a normal string with Unicode characters in it, when I make a match, I get Unicode characters. However, when I grab a string from the onclick value, I get unicode escape sequences. So I'm trying to convert unicode escape sequences back to Unicode characters. I hope this makes sense. Perhaps there is another way to do this.

In the example below, the bar behaves the way I want, 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>
+5
source share
2 answers

, \1 . :

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

$1 - .

, . , \u unicode. , \u \then literal u. \u u.

+3

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

? , \u.

+1

All Articles