regex replace with placeholder - regex 替換包含group使用

把符合hashtag值用span包住

by Alan Tsai

HTML

<div id='divTest'>
Here's a #hashtag and here is #a_tag; which should be different. 
Also testing: Mid#hash. #123 #123 #!@£ and <p>#hash</p>
</div>

<p>
<button id="btnShowResult">
show result
</button>
</p>
<p>
Result:
</p>
<div id="divResult">

</div>

JavaScript

$(function(){
	$("#btnShowResult").click(function(){
		var html = $("#divTest").html().replace(/#(\d*\w+\d*)\b/g, "<span>$1</<span>");
  
  		$("#divResult").text(html);  
  })
})