views:

20

answers:

1

I have the following as3 function below which converts normal html with links so that the links have 'event:' prepended so that I can catch them with a TextEvent listener.

protected function convertLinks(str:String):String
{
  var p1:RegExp = /href|HREF="(.[^"]*)"/gs;
  str = str.replace(p1,'HREF="event:$1"');
  return str;
}

For example

<a href="http://www.somedomain.com"&gt;

gets converted to

<a href="event:http://www.somedomain.com"&gt;

This works just fine, but i have a problem with links that have already been converted.

I need to exclude the situation where i have a string such as

<a href="event:http://www.somedomain.com"&gt; 

put through the function, because at the moment this gets converted to

<a href="event:event:http://www.somedomain.com"&gt;

Which breaks the link.

How can i modify my function so that links with 'event:' at the start are NOT matched and are left unchanged?

+1  A: 

First of all, trying to manipulate HTML with regex may not be a good idea.

That said, according to the flavor comparison chart on regular-expressions.info, ActionScript regex is based off of ECMA engine, which supports lookaheads.

Thus you can write this:

/(?:href|HREF)="(?!event:)(.[^"]*)"/

(?=…) is positive lookahead; it asserts that a given pattern can be matched. (?!…) is negative lookahead; it asserts that a given pattern can NOT be matched.

Note that the inclusion of the . is very peculiar. It's probably not intended to include the . there since it can match a closing doublequote.

Note also that I've fixed the alternation for the href/HREF by using a non-capturing group (?:…).

This is because:

  • this|that matches either "this" or "that"
  • this|that thing matches either "this" or "that thing"
  • (this|that) thing matches either "this thing" or "that thing"

Alternatively you may also want to just turn on case-insensity flag /i, which would handle things like hReF or eVeNt:.

Thus, perhaps your pattern should just be

/href="(?!event:)([^"]*)"/gsi

If lookahead was not supported, you can use an optional pattern that matches event: if it's there, excluding it from group 1, so that it doesn't get included when you substitute in $1.

/href="(?:event:)?([^"]*)"/gsi
       \________/ \_____/
   non-capturing    group 1
     optional
polygenelubricants
polygenelubricants, many thanks, your original line of code did exactly what i needed, as lookahead is supported.Thank you also for the additonal information. RegEx seems to be a very specific way of thinking that i haven't quite got my head round yet. You really need to learn all the syntax properly, so thanks for your detail explanation.
Marin