如果方括号存在,我想从字符串的开头和结尾删除方括号。
[Just a string]
Just a string
Just a string [comment]
[Just a string [comment]]
应该导致
Just a string
Just a string
Just a string [comment]
Just a string [comment]
我试图构建一个正则表达式,但我没有以正确的方式得到它,因为它没有寻找位置:
string.replace(/[\[\]]+/g,'')
原文由 user3142695 发布,翻译遵循 CC BY-SA 4.0 许可协议
应该做的伎俩。
^
匹配字符串的开头$
匹配字符串的结尾。(.+)
匹配两者之间的所有内容,以在最终字符串中报告。