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.

George Profenza
  • 50,687
  • 19
  • 144
  • 218

3 Answers3

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>");
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
  • Thanks that solves the biggest issue. A clear case of RTFM for me here. I could easily put a in front, but any hints on how I can add the closing at the end of the tag ? Sağol! – George Profenza Aug 28 '09 at 09:13
  • 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. – Sinan Ünür Aug 28 '09 at 11:48
  • 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 :( – George Profenza Sep 01 '09 at 14:31
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);
Virusescu
  • 394
  • 1
  • 5
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>');
e-motiv
  • 5,795
  • 5
  • 27
  • 28