Javascript Regex: surround @_____, #_____, and http://______ with anchor tags in one pass? -
related (but slightly different):
< P> I want to surround all instances of
@_______
, #________
, and http: // ________
with an anchor tag . For example, consider this twitter message:
Spread the python on the celestial dog scooter, quick brown fox jumping
Running occurs with the desired Reggae pattern:
Instant brown fox & lt; A href = "a" & gt; @spreadtamovi & lt; / A & gt; Lazy jumps on the dog & lt; A href = "b" & gt; # Cows & lt; / A & gt; & Lt; A href = "c" & gt; Http: // URL & lt; / A & gt; Fill out the words starting with
only @
, #
or http: //
So that Dog@gmail.com
dogs
var sample = "sample"; Sample = sample.rele (/ [^ \ s + - +. +] (@ \ W + | # \ w + | http: // [\ w \ ./] +) [$ \ s + - +. + ] / G, "& lt; a & gt; $ 1 & lt; / a & gt;"); Includes
$ 1
match string.
Using the function (which I recommend for your particular situation):
var sample = "sample"; Sample = sample.rele (/ [^ \ s + - +. +] (@ \ W + | # \ w + | http: // [\ w \ ./] +) [$ \ s + - +. + ] / G, function (str) {var href = ""; if (str.indeoxOf ("#")! == -1) href = str; and if (str.indexOf ("@")! == - 1) .. return "
function, when you want to have more or better control, it is easier if you want links in their anchor tags to have different hrefs.
See more.
Comments
Post a Comment