替换“[]"之间的字符串使用 javascript 使用正则表达式 [英] Replace string between "[]" with javascript using regexp
问题描述
我有一个从网络服务返回的字符串,其中包含像[]"这样的开括号所以字符串看起来像这样:
I have a string coming back from a webservice the contains open brackets like such "[]" so the string would look like something like this:
[1] blabh balh blah
我想编写一个正则表达式来删除[1]"或任何括号之间的内容.现在我已经尝试过类似的东西:
I would like to write a regexp that would remove the "[1]" or anything between open brackets. Right now I've tried something like:
var regexp = /\[[]\\]/g;
但这不起作用.我在这里绊倒了自己的两只脚.我只想找到以["开头并以]"结尾的任何内容,并替换中间的所有内容,包括左括号和右括号.任何帮助或指导将不胜感激.
but this does not work. I'm stumbling on my own two feet here. I simply just want to find anything that starts with "[" and ends with "]" and replace everything in the middle including the open and closed brackets. Any help or guidance would be much appreciated.
推荐答案
这应该有效:
var str = '[1] blabh balh blah';
str = str.replace(/\[.*?\]\s?/g, '');
如果你有嵌套的括号正则表达式可能不是最好的选择.
If you have nested brackets regexp might no be the best option though.
这篇关于替换“[]"之间的字符串使用 javascript 使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!