How to remove square brackets from string using javascript?


Hello, I am working a javascript function, in which I want to remove square brackets from string suppose if string it "[test]", then after removing square brackets I should get "test" only.

How can I do this using Javascript and regex, or without regex?


Asked by:- neena
0
: 71 At:- 7/18/2018 3:16:10 PM
Javascript regex remove square brackets from string






1 Answers
profileImage Answered by:- pika

You can remove Sqaure brackets from string using Regex in Javascript, here is the example of it

var WithOutBrackets="[test]".replace(/[\[\]']+/g,'');

console.log(WithOutBrackets); //test

Addtionally if you string is something like below

var strWithBrackets= "[br549   ] [001] [fkrig  009] [      ] [ 01 ]";

You can change your Regex and make it like

var newString= "[br549   ] [001] [fkrig  009] [      ] [ 01 ] ".replace(/\s|\[|\]/g,"");
console.log(mynewstring);

So,basically output depends on your input string and regex.

2
At:- 7/19/2018 1:15:24 PM
The first method with .replace method works for me as needed, didn't tested other methods, thanks. 0
By : neena - at :- 9/6/2018 10:45:07 AM





Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use

Subscribe Now

Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly