Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove square brackets in string using regex?

['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 "".

like image 540
Mugdha Avatar asked Sep 28 '10 11:09

Mugdha


People also ask

How do you remove square brackets from string?

Brackets can be removed from a string in Javascript by using a regular expression in combination with the . replace() method.

How do you escape square brackets in regex?

If you want to remove the [ or the ] , use the expression: "\\[|\\]" . The two backslashes escape the square bracket and the pipe is an "or".

What do the [] brackets mean in regular expressions?

By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex. Only parentheses can be used for grouping.

How do I remove a specific character from a string in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")


2 Answers

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

/[\[\]']+/g 

Replace with the empty string.

console.log("['abc','xyz']".replace(/[\[\]']+/g,''));
like image 68
Mark Byers Avatar answered Sep 22 '22 09:09

Mark Byers


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

like image 25
mkm Avatar answered Sep 21 '22 09:09

mkm