Am in a process of writing a javascript to replace a text within [] to a html link. But am stuck at generating a regular expression to match any string that is in [] and then replace it with a hyperlink.
Below is my code snippet that i have tried:
<html>
<head>
</head>
<body id="body">
Hello World [1234]<br>
[322]<br>
Hello
</body>
</html>
<script type="text/javascript">
var bodyText=document.getElementById('body').innerHTML;
var pattern = "\[(.*?)\]";
var replaceText = "<a href="www.mysite.com">Pradeep</a>";
document.getElementById('body').innerHTML = bodyText.replace(pattern/gi, replaceText);
</script>
Can anyone please suggest me a best way to do it
Thanks,