Regular expression in as3 to ignore matching with a specific start of line

I have the following as3 function below that converts normal html with links so that the links have an event: event so that I can catch them using 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 instance

<a href="http://www.somedomain.com">

converted to

<a href="event:http://www.somedomain.com">

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

I need to rule out a situation where I have a string such as

<a href="event:http://www.somedomain.com"> 

through a function, because at the moment it is converted to

<a href="event:event:http://www.somedomain.com">

What breaks the connection.

How can I change my function so that the links with "event:" at the beginning do NOT match and remain unchanged?

+3
source share
1 answer

, HTML .

, regular-expressions.info, ActionScript regex ECMA-, lookaheads.

, :

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

(?=…) ; , . (?!…) - ; , .

, . . , ., .

, alternation href/HREF, (?:…).

, :

  • this|that "this", "that"
  • this|that thing "this", "that thing"
  • (this|that) thing "this thing", "that thing"

case /i, , hReF eVeNt:.

, ,

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

lookahead , optional, eVeNt:, , 1, $1.

/href="(?:event:)?([^"]*)"/gsi
       \________/ \_____/
   non-capturing    group 1
     optional
+4

Source: https://habr.com/ru/post/1761304/


All Articles