How do I linkify text using ActionScript 3

后端 未结 1 716
名媛妹妹
名媛妹妹 2020-12-11 13:52

I have a text that I want to linkify (identify URLs and convert them to HTML links). The text could be multi-line, and could contain multiple urls like the example below.

相关标签:
1条回答
  • 2020-12-11 14:33

    ooph ... why does everybody use regex these days, to accomplish super simple tasks? also, you forgot, that "+" is a valid character for URLs, as a replacement for space, and even an awful lot of other characters may be used, so your pattern would not even match accordingly ...

    well, anyway, have a look at AS3 regex metacharacters ... that'll GREATLY improve your expression's readability and is much more robust...

    i'd go with something like this, really:

    var r:RegExp = /(?:http|https):\/\/\S*/g;
    trace(str.replace(r, function (s:String,...rest):String { 
        return '<a href="' + s + '">' + s + '</a>' 
    } ));
    

    but the actual point, was the global flag ...

    good luck then ... :)

    greetz

    back2dos

    0 讨论(0)
提交回复
热议问题