开发者

how do I replace all the links in a text using regex in as3? [duplicate]

开发者 https://www.devze.com 2022-12-11 11:33 出处:网络
This question already has an answer here: Closed 10 years ago. Possible Duplicate: How do I linkify text using ActionScript 3
This question already has an answer here: Closed 10 years ago.

Possible Duplicate:

How do I linkify text using ActionScript 3

I am using a regular expression to find links in a generic string and highlight that text(underline, a href, anything).

Here's what I have so far:

var linkRegEx:RegExp = new RegExp("(https?://)?(www\\.)?([a-zA-Z0-9_%]*)\\b\\.[a-z]{2,4}(\\.[a-z]{2})?((/[a-zA-Z0-9_%]*)+)?(\\.[a-z]*)?(:\\d{1,5})?","g");
var link:String = 'generic links: www.google.com http://www.yahoo.com  stackoverflow.com';
link = addLinks(linkRegEx,link);
textField.htmlText = link;//textField is a TextField I have on stage

function addLinks(pattern:RegExp,text:String):String{
    while((pattern.test(tex开发者_开发技巧t))!=false){
        text=text.replace(pattern, "<u>link</u>");
    }
    return text;
}

I get all the text replaced with "link". I'd like to have the same text that was matching the expresion instead of "link". I tried

text=text.replace(pattern, "<u>"+linkRegEx.exec(text)[0]+"</u>");

but I ran into trouble. I don't think I fully understand how regex and the replace method work.


Ok, I've read the documentation for the replace() method.

There are two key things:

  1. You can use $& to get the matched substring. A lot of handy and weird looking symbols there.
  2. Use a second string when replacing, otherwise you end up in an endless loops and tiny black wholes keep spawning every now and then.

Here's how the correct version of the function looks:

function addLinks(pattern:RegExp,text:String):String{
    var result = '';
    while(pattern.test(text)) result = text.replace(pattern, "<font color=\"#0000dd\"><a href=\"$&\">$&</a></font>");
    if(result == '') result+= text;//if there was nothing to replace
    return result;
}

As Cay mentioned, a stylesheet is more apropiate for styling. Thanks for the input.

UPDATE

The RegEx listed above doesn't work when links contain the # sign. Here's an updated version of the function:

function addAnchors(text:String):String{
    var result:String = '';
    var pattern:RegExp = /(?<!\S)(((f|ht){1}tp[s]?:\/\/|(?<!\S)www\.)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/g;
    while(pattern.test(text)) result = text.replace(pattern, "<font color=\"#0000dd\"><a href=\"$&\">$&</a></font>");
    if(result == '') result+= text;//if there was nothing to replace
    return result;
}


I read here that in AS3 you have a replace function where you can pass a callback performing custom manipulation. This looks even more flexible than using standard regex capture groups.


If you just need to underline all the links in your textfield, the propper way to do it should be using StyleSheet... try with something like this:

var style:StyleSheet = new StyleSheet();
style.setStyle("a", {textDecoration:"underline"});
tf.styleSheet=style;
tf.htmlText="hello <a href='#test'>world</a>";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号