在ActionScript正则表达式中转义多个字符 [英] Escaping multiple characters in an ActionScript regular expression
本文介绍了在ActionScript正则表达式中转义多个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有特殊的字符串,例如Java中的\Q和\E?
谢谢。
Are there special strings for that, such as \Q and \E in Java? Thanks.
推荐答案
据我所知,没有等同于 \Q
和 \E
在AS3 RegExp中。您可以做的与相同的事情 :转义在 RegExp
模式中使用的特殊字符:
As far as I know there is no equivalent to \Q
and \E
in AS3 RegExp. What you can do is the same thing you would in Javascript: escape special characters for use within a RegExp
pattern:
function escapeForRegExp(s:String):String {
return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, "\\$&");
}
// Example:
var s:String = "Some (text) with + special [regex] chars?";
var r:RegExp = new RegExp("^" + escapeForRegExp(s), "g");
// Same as if you wrote the pattern with escaped characters:
/^Some \(text\) with \+ special \[regex\] chars\?/g
这篇关于在ActionScript正则表达式中转义多个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文