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">
gets converted to
<a href="e开发者_如何学Pythonvent:http://www.somedomain.com">
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">
put through the function, because at the moment this gets converted to
<a href="event:event:http://www.somedomain.com">
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?
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|thatmatches either"this"or"that"this|that thingmatches either"this"or"that thing"(this|that) thingmatches 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
加载中,请稍侯......
精彩评论