Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>[[EDIT]]</p> <p>Line 32 changed from:</p> <pre><code>last = lowest.end; </code></pre> <p>to:</p> <pre><code>last = lowest.end + idx; </code></pre> <hr> <p>As I mentioned in the comment...really blows the code out a little. The alternative is to pull in an existing AS2 regexp library. Last I looked (5 years ago), the AS2 regexp implementations were not 100% with regards to expression support. I'm sure there is a shorter way of going about this, but here is the code after a first attempt:</p> <pre><code>String.prototype.replace = function(search:String, replace:String):String { return this.split(search).join(replace); } function linkPaths(text:String, template:String):String { var idx:Number; var lowest:Object; var selectors:Array = [ "http://", "www." ]; var sub:String; var last:Number = 0; var result:String = ""; for(idx=0; idx&lt;text.length; idx++) { sub = text.substring(idx); lowest = findLowestSelector(sub, selectors); if(!lowest) { break; } result += text.substring(last, lowest.start + idx) + template.replace("${url}", sub.substring(lowest.start, lowest.end)); last = lowest.end + idx; idx += lowest.end; } return result; } function findLowestSelector(text:String, selectors:Array):Object { var idx:Number; var index:Number; var start:Number = Number.MAX_VALUE; var end:Number = -1; for(idx=0; idx&lt;selectors.length; idx++) { index = text.indexOf(selectors[idx]); if ( index != -1 &amp;&amp; index &lt; start ) { start = index; index = text.indexOf(" ", start); end = index == -1 ? text.length : index ; } } return start != -1 ? { start: start, end: end } : null ; } trace(linkPaths ( "test text here http://www.test.com is a link, along with www.test.com", "&lt;a href='${url}' target='_blank'&gt;&lt;u&gt;&lt;font color='#666666'&gt;${url}&lt;/font&gt;&lt;/u&gt;&lt;/a&gt;" )); </code></pre> <p>Let me know if there are any issues. The findLowestSelector method uses a single blank space, or end of line to dictate the end value in the return object.</p> <p>Been a while since I've worked with AS2...</p> <p>Best of luck!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload