Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
68
rated 0 times [  73] [ 5]  / answers: 1 / hits: 16724  / 8 Years ago, wed, july 13, 2016, 12:00:00

I would like to remove square brackets from beginning and end of a string, if they are existing.



[Just a string]
Just a string
Just a string [comment]
[Just a string [comment]]


Should result in



Just a string
Just a string
Just a string [comment]
Just a string [comment]


I tried to build an regex, but I don't get it in a correct way, as it doesn't look for the position:



string.replace(/[[]]+/g,'')

More From » regex

 Answers
10
string.replace(/^[(.+)]$/,'$1')


should do the trick.




  • ^ matches the begining of the string

  • $ matches the end of the string.

  • (.+) matches everything in between, to report it back in the final string.


[#61389] Tuesday, July 12, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mariann

Total Points: 201
Total Questions: 133
Total Answers: 107

Location: Czech Republic
Member since Thu, Aug 11, 2022
2 Years ago
;