RegEx to modify urls in htmlText as3

匿名 (未验证) 提交于 2019-12-03 08:30:34

问题:

I have some html text that I set into a TextField in flash. I want to highlight links ( either in a different colour, either just by using underline and make sure the link target is set to "_blank".

I am really bad at RegEx. I found a handy expression on RegExr :

 </?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?> 

but I couldn't use it.

What I will be dealing with is this:

<a href="http://randomwebsite.web" /> 

I will need to do a String.replace()

to get something like this:

<u><a href="http://randomwebsite.web" target="_blank"/></u> 

I'm not sure this can be done in one go. Priority is making sure the link has target set to blank.

回答1:

I recomend you this simple test tool http://www.regular-expressions.info/javascriptexample.html

Here's a working example with a more complex input string.

var pattern:RegExp = /<a href="([\w:\/.-_]*)"[ ]* \/>/gi; var str:String = 'hello world <a href="http://www.stackoverflow.com/" /> hello there'; var newstr = str.replace(pattern, '<li><a href="$1" target="blank" /></li>'); trace(newstr); 


回答2:

I do not know how Action Script regexes work, but noting that attributes can appear anywhere in the tag, you can substitute <a target="_blank" href= for every <a href=. Something like this maybe:

var pattern:RegExp = /<a\s+href=/g; var str:String = "<a href=\"http://stackoverflow.com/\">"; str.replace(pattern, "<a target=\"_blank\" href=");   

Copied from Adobe docs because I do not know much about AS3 regex syntax.

Now, manipulating HTML through regex is usually very fragile, but I think you can get away with it in this case. First, a better way to style the link would be through CSS, rather than using the <font> tag:

str.replace(pattern, "<a style=\"color:#00d\" target=\"_blank\" href=");   

To surround the link with other tags, you have to capture everything in <a ...>anchor text</a> which is fraught with difficulty in the general case, because pretty much anything can go in there.

Another approach would be to use:

var start:RegExp = /<a href=/g; var end:RegExp = /<\/a>/g; var str:String = "<a\s+href=\"http://stackoverflow.com/\">"; str.replace(start, "<font color=\"#0000dd\"><a target=\"_blank\" href=");   str.replace(end, "</a></font>"); 

As I said, I have never used AS and so take this with a grain of salt. You might be better off if you have any way of manipulating the DOM.

Something like this might appear to work as well:

var pattern:RegExp = /<a\s+href=(.+?)<\/a>/mg; ... str.replace(pattern,      "<font color=\"#0000dd\"><a target=\"_blank\" href=$1</a></font>"); 


回答3:

What about this? I needed this for myself and it looks for al links (a-tags) with ot without a target already.

var pattern:RegExp = /<a (  ( [^>](?!target) )*  ) ((.+)target="[^"]*")*  (.*)<\/a> /xgi; str.replace(pattern, '<a$1$4 target="_blank"$5<\/a>'); 


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