在 actionscript 正则表达式中指定 unicode 范围 [英] Specifying a unicode range in an actionscript regular expression
问题描述
我一直在尝试编写一个正则表达式来匹配所有 unicode 单词字符,例如:
I have been trying to write a regular expression that would match all unicode word character something like :
/[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF\w]/gi
但这完全失败并且不匹配任何东西.我尝试了各种表达式,似乎只要我尝试指定一个范围,它就会失败.有人比我幸运吗?
But this completely fails and doesn't match anything. I have tried a variety of expressions and it seems that as soon as I try to specify a range it fails. As anyone been luckier than me?
我希望 actionscript 能提供类似 \p{L} 的东西,但如果有类似的东西,我在文档中找不到它.
I wish actionscript would offer something like \p{L}, but if there's anything in the like, I couldn't find it in the doc.
推荐答案
您可以将 String.fromCharCode 与 unicode 字符一起使用,然后范围将在正则表达式中正常工作.以下是使用原始问题的示例:
You can use String.fromCharCode with the unicode characters and then the ranges will work correctly in a regular expression. Here is an example using your original problem:
var exp:RegExp = new RegExp("[" + generateRangeForUnicodeVariables(0x00A0, 0xD7FF) + generateRangeForUnicodeVariables(0xF900, 0xFDCF) + generateRangeForUnicodeVariables(0xFDF0, 0xFFEF) + "\w]", "gi");
private function generateRangeForUnicodeVariables(var1:Object, var2:Object):String
{
return String.fromCharCode(var1) + "-" + String.fromCharCode(var2);
}
这篇关于在 actionscript 正则表达式中指定 unicode 范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!