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
Post a Comment