when I use var j= new RegExp('('+val+')','gi') then $1 works fine. but when I am using it without bracket then it is not working. So I want to know why brackets is necessary and does $1 hold the value which have to be replaced?
var val='city'
var j= new RegExp('('+val+')','gi')
console.log(j)
$('div').html(function(i,val){
return val.replace(j,'<span>$1</span>')
})