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

亡梦爱人 提交于 2019-12-29 02:01:13

问题


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(text))!=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.


回答1:


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;
}



回答2:


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.




回答3:


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>";


来源:https://stackoverflow.com/questions/1748569/how-do-i-replace-all-the-links-in-a-text-using-regex-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!