views:

58

answers:

3

['abc','xyz'] – this string I want turn into abc,xyz using regex in javascript. I want to replace both open close square bracket & single quote with empty string ie "".

+2  A: 

Use this regular expression to match square brackets or single quotes:

/[\[\]']+/g

Replace with the empty string.

"['abc','xyz']".replace(/[\[\]']+/g,'')
Mark Byers
thank you mark it is working
Mugdha
A: 

str.replace(/[[\]]/g,'')

ithkuil
+1  A: 

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.

Greg Hewgill
It's not JSON because JSON uses double quotes for strings, not single quotes. See: http://www.json.org/
Mark Byers
Oh, true. But it still works. :)
Greg Hewgill