Javascript Regex: surround @_____, #_____ and http: // ______ with anchor tags in one go?

Related (but slightly different):

Surround all instances of @________, #___________ and http: // _________ with anchor tags?

I would like to combine all instances @_______, #________and http://________with anchor tags in a single pass.

For example, consider this Twitter post:

@Spreadthemovie fast brown fox jumps through a lazy dog ​​#cow http: // URL

Running with the desired regular expression pattern will give:

The quick brown fox <a href="a">@Spreadthemovie</a> jumps over the lazy
dog <a href="b">#cow</a> <a href="c">http://URL</a>

Only surrounding words starting with @, #or http://so as dog@gmail.comnot to become dog<b>@gmail.com</b>.

+5
source share
2
var sample = "@sample";
sample = sample.replace(/[^\s+-+.+](@\w+|#\w+|http://[\w\./]+)[$\s+-+.+]/g, "<a>$1</a>");

$1 .

( ):

var sample = "@sample";
sample = sample.replace(/[^\s+-+.+](@\w+|#\w+|http://[\w\./]+)[$\s+-+.+]/g, function(str) {
    var href="";
    if(str.indeoxOf("#") !== -1)
        href=str;
    else if(str.indexOf("@") !== -1)
      ...
    return "<a href="+href+">"+str+"</a>";
});

- , . , , href .

, MDC.

+2
s/(?<!\w)(@\w*|#\w*|http:\/\/[\w\/\.?=]*\w)/<a>$1<\/a>/g

, . @..., #... http... , . , .

URL, , . . , , , .

, @, # http..., , .

:

s/(?<!\w)(http:\/\/[\w\/\.?=]*\w)/<a href="c">$1<\/a>/g
s/(?<!\w)(#\w*)/<a href="b">$1<\/a>/g
s/(?<!\w)(@\w*)/<a href="a">$1<\/a>/g
+1

All Articles