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?
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.
You can also try 'str.replace(/[[\]]/g,'')
' to remove '[' ']' brackets from string.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly