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

Glib GIOChannel -

jsp - java.sql.SQLException: [Microsoft][ODBC Microsoft Access > Driver] Too few parameters. Expected 1 -

delphi - When I encode/decode SMS PDU (GSM 7 Bit) user data, do I need prepend the UDH first? -