javascript - Search and replace a text sequence in html -


i trying search , replace text sequence in website. webpage has text "#45421" (# followed 5 digits). need replace <a href="http://example.com/45421">#45421</a>. tried below code this,

var mystring = $('.content').html(); var re = /\b\#d{5}\b/g; mystring.replace(re, "<a href='http://example.com/'"+re+">"+re+"</a>"); 

but unable desired results. see text sequence replaced links script executed. script injected page chrome extension. missing here?

you need use regex capturing groups - put parentheses around part of regex want capture, , refer in replacement string $1 (and, if you're capturing multiple, next $2 , on):

mystring.replace(/#(\d{5})\b/g, '<a href="http://example.com/$1">#$1</a>'); 

your regex has misplaced escape backslash - d should escaped, not #


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -