[英]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:

我有一些javascript讀取一些cookie,並做一些匹配。我正在使用分組來捕獲正則表達式的一部分並在以后使用它們。我遇到了unicode字符的問題。如果我有一個像\u25BA►這樣的字符,當我在分組中找到這個字符時,它會返回字符串'\ u25BA'而不是我正在尋找的unicode字符。如果我指定了角色,我可以解決問題,但我不能讓它更普遍地工作。以下將按我的要求工作:

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.


<span id='foo' onclick='expandCollapse(event, "►", "▼");'>foo</span>
<script type='text/javascript'>
var foo= document.getElementById('foo').onclick+'';
var foomatches=foo.match(/.*\(event, ?"([^"]*)", ?"([^"]*)".*\)/);
var bar='expandCollapse(event, "►", "▼");'
var barmatches=bar.match(/.*\(event, ?"([^"]*)", ?"([^"]*)".*\)/);

2 个解决方案



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

我認為你的\ 1是錯的。應該是這樣的:

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

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

$ 1是替換文本中的反向引用。

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.

但我不完全確定你要做什么。請記住\ u只能與unicode文字一起使用。所以正則表達式中的\ u匹配文字\然后是文字u。並且替換文本中的\ u是你的。



what about

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

? I don't think you need the escaped \u there.




粤ICP备14056181号  © 2014-2020