Sometimes, we want to remove square brackets in string using regex with JavaScript.
In this article, we’ll look at how to remove square brackets in string using regex with JavaScript.
How to remove square brackets in string using regex with JavaScript?
To remove square brackets in string using regex with JavaScript, we call the string replace
method.
For instance, we write
console.log("['abc','xyz']".replace(/[\[\]']+/g, ""));
to call replace
with a regex that matches opening and closing square brackets with \[
and \]
.
We use the g
flag to replace all matches with empty strings.
The string with the replacements done is returned.
Conclusion
To remove square brackets in string using regex with JavaScript, we call the string replace
method.