Reputation: 320
I have a large blocks or text on a message board that will contain strings beginning with #. Can I use jquery to replace these with a link containing the string?
For example, changing:
<div id="mydiv">This is my #block of text with #multiple hash strings</div>
to:
<div id="mydiv">This is my <a href="newpage.asp?block">#block</a> of text with <a href="newpage.asp?multiple">#multiple</a> hash strings</div>
Thanks in advance!
Upvotes: 2
Views: 2465
Reputation: 885
This will do the work:
$('*:contains("#")').each(function(){
if($(this).children().length < 1)
$(this).html(
$(this).text().replace(
/#(.*)#/
,'<a href=newpage.asp?'+$(this).text()+'>'+$(this).text()+'</a>'
)
)
});
Upvotes: 0
Reputation: 349062
You could use the following code to replace strings:
var string = "#has etc #has ";
string = string.replace(/#(\S+)/g, '<a href="newpage.asp?$1">#$1</a>');
Upvotes: 4