0

I am really bad when it comes to using regexp, so please bear with me on this one.

I have piece of ActionScript code which is supposed to evaluate a string of HTML and break it up into individual pieces. So a string like <p>Hi</p><span>Hi</span><a href="index.php">Hi</a> would be translated into:

1. <p>Hi</p>
2. <span>Hi</span>
3. <a href="index.php">Hi</a>
...

However, when I run a test version of this code, I get a value of null in return. I'm pretty sure my regexp string is good, but I'm doing something wrong in ActionScript. Could you please point in the right direction? My code is below:

var evaluatedInput:RegExp = new RegExp('/<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\1>/');
var output:Object = evaluatedInput.exec("<p>Hi</p><span>Hi</span><a href=\"index.php\">Hi</a>");
trace(output);

Thank you for your time,
spryno724

2 Answers 2

1

In ActionScript you're supposed to create a RegExp object one of two ways. You can enclose the expression in /.../ delimiters to form a regex literal:

/<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>/gi

...or you can write it as a string literal, which you pass to the the RegExp constructor:

new RegExp('<([A-Z][A-Z0-9]*)\\b[^>]*>(.*?)</\\1>', 'gi')

You seem to be using an amalgam of the two methods and getting garbage as a result. Some other points of interest:

  • Because regex literals use forward-slash as the delimiter, any / in the regex itself needs to be escaped with a backslash, e.g., <\/\1>

  • In the string version it's the backslash you have to escape (e.g., </\\1>). Otherwise the AS compiler tries to treat it as part a string-literal escape sequence like \" or \n. In your code, the \b represents a backspace, not a word boundary, and \1 is probably treated as a syntax error, not a back-reference as you intended.

  • Your regex needs both the g ("global") and i ("ignore-case") modifiers; I've demonstrated how to apply them.

Sign up to request clarification or add additional context in comments.

2 Comments

Wow, thanks Alan. I'm getting closer, but, due to my poor regexp understanding, I'm not quite there. Using your regexp selector, I'm getting the following output <p>Hi</p>,p,Hi. It is possible to get only <p>Hi!</p>,<span>Hi</span>,<a href="index.php">Hi</a> and not just the tag or tag contents?
Never mind, I got it! Thanks for fixing the regexp and for the good explanation!
1

Example Usage

Adapted from here
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/RegExp.html#exec()

     var myPattern:RegExp = /\>\</g;  
     var str:String = "<p>Hi</p><span>Hi</span><a href=\"index.php\">Hi</a>";
     var result:Object = myPattern.exec(str);

     //To loop through all results manually
     while (result != null) {             
         trace ( result.index, "\t", result);            
         result = myPattern.exec(str);
     }

     //or, just replace. Note this does not required the myPattern.exec(str);
     str.replace(myPattern, ">\n<");

Original Answer

See this answer:

AS3 RegEx returns null

At the very least, the tool from gSkinner should be a solution to your issue(s).

Specifically, to do what you want to do, you would use the following regex expression:

/\>\</g

And on your matches, use the index value, and replace with:

>\n<

You can test this yourself on the gskinner Regexr tool using the Replace tab.

6 Comments

Thank your for your help, but my regexp skills are so poor right now that I am having a hard time following you. Could you post a corrected version of the ActionScript regexp selector I posted above?
Hmm... thanks for the update, but I tried your example and it didn't return anything. :(
Sorry wasn't done. Fully updated, should work fine. I've tested, works as expected.
< and > have no special meaning in AS3 regexes (or in most other flavors, either). But in some flavors, \< and \> do have special meanings (i.e., word boundaries), so it's best not to escape them.
Thanks Alan, you're right... although escaping them doesn't hurt either. :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.