[\'abc\',\'xyz\'] – this string I want turn into abc,xyz using regex in javascript. I want to replace both open close square bracket & single q
Use this regular expression to match square brackets or single quotes:
/[\[\]']+/g
Replace with the empty string.
"['abc','xyz']".replace(/[\[\]']+/g,'')
You probably don't even need string substitution for that. If your original string is JSON, try:
js> a="['abc','xyz']"
['abc','xyz']
js> eval(a).join(",")
abc,xyz
Be careful with eval, of course.
here you go
var str = "['abc',['def','ghi'],'jkl']";
//'[\'abc\',[\'def\',\'ghi\'],\'jkl\']'
str.replace(/[\[\]']/g,'' );
//'abc,def,ghi,jkl'
str.replace(/[[\]]/g,'')