我想删除给定字符串中的 [] 符号及其内部的任何内容:
const text = "This should [but this should be removed with the brackets] stay intact."
const result = text.replace(/\[.*?\]/g,'')
console.log(result);
问题是删除括号后剩余的多余空间。
我该如何解决这个问题?
最佳答案
我可以选择匹配模式两侧的空格,然后替换为单个空格:
const text = "This should [but this should be removed with the brackets] stay intact."
const result = text.replace(/ ?\[[^\]]*\] ?/g,' ')
console.log(result);
关于javascript - 使用正则表达式删除括号和其中的文本,无需额外的剩余空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65382483/