Javascript匹配並用unicode替換

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

更多信息:感謝您的回答。讓我再添加一些背景知識。我認為問題在於我從一個跨度的“onclick”獲得我的比賽。我在下面列出了一個稍微詳細的例子。如果我有一個帶有unicode字符的普通字符串,當我進行匹配時,我會得到unicode字符。但是,當我從onclick的值中獲取字符串時,我得到了unicode轉義序列。所以我一直在嘗試將unicode轉義序列轉換回unicode字符。我希望這是有道理的。也許還有另一種方法可以做到這一點。

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

在下面的示例中,bar的行為與我想要的一樣,而foo則不行。

<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>

2 个解决方案

#1


3  

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是你的。

#2


1  

what about

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

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

?我認為你不需要逃脫。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/05/27/724eb97aa445dbbd18f6e671f39b809b.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com