How to remove square brackets in string using regex?

前端 未结 4 1206
天涯浪人
天涯浪人 2020-11-29 08:47

[\'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

相关标签:
4条回答
  • 2020-11-29 09:28

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

    /[\[\]']+/g
    

    Replace with the empty string.

    "['abc','xyz']".replace(/[\[\]']+/g,'')
    
    0 讨论(0)
  • 2020-11-29 09:31

    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.

    0 讨论(0)
  • 2020-11-29 09:39

    here you go

    var str = "['abc',['def','ghi'],'jkl']";
    //'[\'abc\',[\'def\',\'ghi\'],\'jkl\']'
    str.replace(/[\[\]']/g,'' );
    //'abc,def,ghi,jkl'
    
    0 讨论(0)
  • 2020-11-29 09:49

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

    0 讨论(0)
提交回复
热议问题