views:

1216

answers:

3

Hi,

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>

Any help is appreciated..

A: 

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

Daniel Lew
Thanks. Already tested (?:) but does not work..
Could you paste the code you're using to do the regex?
Daniel Lew
The regex codes I tried are in my post and also tried yours before.. They didn't work. But the problem is solved now. Thanks for your time..
A: 

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

quoo
Thanks a lot quoo. That helped me solve the problem..The solution is below..
A: 

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

yay, glad to help, you could also do:var re:RegExp = new RegExp(/<body\b[^>]*>(.*?)<\/body>/gis); }return strHTML.replace(re, "$1");and then you're not saving the body open and close tags... but I suppose that doesn't matter too much.
quoo
Sure.. I set 3 groups beacuse in the function I give the user the choice to include tags or not.. If tags are included; I return "$1\r\n$2\r\n\$3".
anyway.. thanks again..