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

Related (but slightly different):

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

I would like to surround all instances of @_______, #________ and http://________with anchor tags. A few passes are okay with me.

For example, consider this Twitter post:

The quick brown fox @Spreadthemovie jumps over the lazy dog #cow, http://bit.ly/bC9Dy

Running with the desired regular expression pattern will give:

The quick brown fox <a href="blah/Spreadthemovie">@Spreadthemovie</a> jumps over the lazy
dog <a href="blah/cow">#cow</a>, <a href="blah/http://bit.ly/bC9Dy">http://bit.ly/bC9Dy</a>

Only surrounding words starting with @, # or http: // so dog@gmail.com will not become dog@gmail.com. Also pay attention to how " #cow," turned into " <a href=urlB>#cow</a>," ... I want only alpha-numeric characters to be at the end of each anchored labeled substring. Also note the href attribute.

If possible, include the actual javascript code with the regex template and replace the function.

Many thanks! This problem haunted me for a while

-1
source share
5 answers

In my code I have a similar function, you can take a look and change it according to your needs:

function checkChatUrl ($ matches)
{
    if(strpos($matches[0],'http://www.xxx.pl/?task=forum')!==false) $n='>forum';
    elseif(strpos($matches[0],'http://www.xxx.pl')!==false) $n='>xxx';
    elseif(strpos($matches[0],'db.php')!==false) return "";
    elseif(strpos($matches[0],'%22')!==false) return "";
    else $n=">".substr($matches[1].$matches[2],0,10).((strlen($matches[1].$matches[2])>10)?'..':'');
    return "<a href='http://$matches[1]$matches[2]' target=_blank $n</a>";
}

$text=preg_replace_callback("/\bhttp:\/\/([\w\.]+)([\#\,\/\~\?\&\=\;\-\w+\.\/]+)\b/i",'checkChatUrl',$text);

URL , URL .

+1
str.replace(
    /(\s|^)([#@])([\w\d]+)|(http:\/\/\S+)/g,
    '$1<a href="$3$4">$2$3$4</a>'
);
+1

@ #, \w metapattern ( , , /). , , - :

(@\w+)
(#\w+)

URL- , , http://, -whitespace:

(http://\S+)

URL- , URL-. , , URL-, :

(http://[a-zA-Z0-9+$_.+!*'(),#/-]+)
0

, . / .

3 3- . (\ b\B), , (^ |\s). , , @tweet, # tag

<script type=text/javascript>
function addTags(str) {
    return str.replace(/\B(@)(\w+)/g, '<a href"//twitter.com=/$2">$1$2</a>')
              .replace(/\B(#)(\w+)/g, '<a href="web#q=$2">$1$2</a>')
              .replace(/\b(http:\S+[^,.])/g, '<a href="$1">$1</a>')
              ;
}
function testTags() {
    document.getElementById('outstr').innerHTML =
    document.getElementById('outtxt').innerHTML =
        addTags(document.getElementById('instr').value);
}
</script>
<input type=text size=100 id="instr" value="@begin ignore@email.com and then #cow to http://mysite.com and also http://yoursite.com."><br>
<p><textarea id="outtxt" cols=90></textarea>
<p id=outstr></p>
<p><button onclick="testTags();">TEST</button>

.

0

!

, .

HTML . XSS- . - , , , , - , - , , XSS, .

A naive replacement of the count allows you to allow embedding arbitrary HTML into your site.

At the very least, try to make sure that what you receive <a href=''>does not start with javascipt:, since you would be open to Cross -Learn more about applying .

0
source

All Articles