RegExp in ActionScript 3: How to exclude a complex prefix?

£可爱£侵袭症+ 提交于 2019-12-25 04:16:13

问题


AS3 RegExp engine (and ECMAScript based JavaScript) do not support complex "lookbehind" expressions. (lookahead expressions are fully supported.)

For example:

 (?<=<body>)(.*?)(?=<\/body>)

will work but;

(?<=<body\b[^>]*>)(.*?)(?=<\/body>)

will not work in AS3.

What I need is to match a complex prefix but exclude it in the final match. In the example above; I'm trying to get the body contents in an HTML text but NOT the opening and closing body tags. And the actual test text looks like this:

<body bgcolor="#EEEEEE">
Some content here...
</body>

回答1:


I think you want var regExp:RegExp = /<body>(.*?)<\/body>/i; as opposed to the 3 groups in your current regexp, so you're only capturing the body tag, you can then reference the match with either \1 or $1 depending on which function you're using:

http://livedocs.adobe.com/flex/3/html/help.html?content=12_Using_Regular_Expressions_09.html




回答2:


This is coming from my JavaScript RegExp experience, but it should be relatively similar...

I don't think you need look-behind, you just need non-capturing groups. They still match input, but they aren't captured by the match:

(?:<body\b[^>]*>)(.*?)(?:<\/body>)

When you do the match, the returned matches will only include the contents (but not the body opening/closing tags).




回答3:


Thanks to quoo; the problem is solved in no time..

var re:RegExp = new RegExp(/(<body\b[^>]*>)(.*?)(<\/body>)/gis); }
return strHTML.replace(re, "$2");

This returns only the content without the body tags. No need to worry about lookbehinds and/or lookaheads..

Thanks to you all..



来源:https://stackoverflow.com/questions/900500/regexp-in-actionscript-3-how-to-exclude-a-complex-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!