开发者

ActionScript HTML Regexp Selector

开发者 https://www.devze.com 2023-02-28 21:17 出处:网络
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 pie

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开发者_StackOverflow社区 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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消