This is possibly a duplicate but I just cant figure out what is wrong with this regular expression.
<p id="demo">Result comes here:</p>
<button onclick="myFunction()">Check</button>
<script>
function myFunction()
{
var str = "The best things in life are free";
var sTerm="things";
var regXSearch= "/\\b"+sTerm+"/gi";
var regX = new RegExp(regXSearch);
//var regX= new RegExp(/\bthings/gi);
var result = regX.test(str);
document.getElementById("demo").innerHTML=result;
}
</script>
For some reason this returns false. If I check the value of the regXSearch variable it will be exacly the same as what is in the regX RegExp that is commented out. And that is in fact returning true.