且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在ActionScript正则表达式中转义多个字符

更新时间:2022-01-07 21:46:42

据我所知,没有等同于 \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