regex - Inline Regular Expressions in JavaScript -


i trying replace occurrences of character in string. works when use regexp() object create regular expression :

var str = "a-b-c-d";  var regex = new regexp('\-','g');  str.replace(regex,'@'); 

so works , "a@b@c@d".

what if want use inline regular expression , say:

str.replace("/\-/g",'@') 

it not work. how do without using regexp();

remove quotes (regex literal not string literal):

str.replace(/\-/g,'@') 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -