1

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
  • 1
    have you considered using stylesheets? Commented Aug 28, 2009 at 21:27

3 Answers 3

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>");
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks that solves the biggest issue. A clear case of RTFM for me here. I could easily put a <font color=#0000DD>in front, but any hints on how I can add the closing </font> at the end of the <a /> tag ? Sağol!
Bir şey değil. That was a nice touch. BTW, I did not mean RTFM. I just had to read them because I have never used AS before.
Thanks again and sorry for the trouble. It's annoying to work with replace because it replaces just the first occurance of the regex, not all of them :(
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);

1 Comment

that old school split/join action solved the replace problem.
0

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>');

Comments

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.