I want to match certain numbers from the page and the regex I created is /^(0 ?){1}([1-8] ?){1}(\d ?){9}$/ and it works fine in online regex tester tool. However, when I put it in my javascript function for some reason the regex matching with ^ & $ is not working in my code.
var regex = /^(0 ?){1}([1-8] ?){1}(\d ?){9}$/;
var text = $("body:first").html();
text = text.replace(regex, "<a href=\"tel:$&\">$&</a>");
$("body:first").html(text);
.html()returns the html tags and not just the inner text and you have RegEx start and end of string anchors, so nothing will ever match. Try using$.trim($("body:first").text())or remove the anchors altogether.(0 ?){1}is the same as(0 ?). The{1}is not needed.