Categories
JavaScript Answers

How to remove square brackets in string using regex with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *