hi i am trying to highlight links in a text using RegEx ,For this I tried with match() method and replace() method,
but i didn’t get it.plz any one can help me.
<head runat="server">
<title></title>
<script type="text/javascript" >
function findReplace() {
var srcString = document.getElementById('new').innerHTML;
var pattern = new RegExp("<[^<]+</a>", 'ig');
var newString = srcString.match(pattern);
for (var i = 0; i < newString.length; i++) {
var replaced = srcString.replace(pattern, '<div style="background-color: yellow; display: inline; font-weight: bold;"> ' + newString[i] + ' </div>')
document.getElementById('new').innerHTML = replaced
}
</script>
</head>
<body>
<form id="form1" runat="server">
<input type='button' id='btn'value='Match' onclick="findReplace()" />
<div id='new'>
Finally we have succeeded! <a href='#'>Remember</a> that if you just want to replace one word,
you should use a string or normal regular expression as your searchFor parameter. However,
if you want to replace <a href='#'>google</a> be sure to write a <a href='#'>regularexpression</a> and append a little g at the end!
</div>
</form>
</body>
</html>
based on your work, here is a solution :
If I were you, I would use the DOM with document.getElementsByTagName(‘a’) instead of Regex + innerHTML property and your regex won’t match :