javascript - Create a Regular expression using the gi flags from a variable -


var metin = "baris"; var icerikoku = "hello baris. surname baris. bye bye baris. how metin."; var n = icerikoku.replace(/metin/gi, "ziya"); $("#icerikoku").html(n); 

print:
hello baris. surname baris. bye bye baris. how ziya.

but, must be:
hello ziya. surname ziya. bye bye ziya. how metin.

you need use regexp constructor that:

var n = icerikoku.replace(new regexp(metin, 'gi'), "ziya"); 

this allow pass variables regexp.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -