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

All overlapping substrings matching a java regex -

c++ - Using OpenSSL in a multi-threaded application -

php - Deleting/Renaming a locked file -