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
Post a Comment