admin管理员组文章数量:1301575
I have a string ing back from a webservice the contains open brackets like such "[]" so the string would look like something like this:
[1] blabh balh blah
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.
I have a string ing back from a webservice the contains open brackets like such "[]" so the string would look like something like this:
[1] blabh balh blah
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.
Share edited Feb 21, 2013 at 3:19 Jess 25.1k21 gold badges130 silver badges155 bronze badges asked Feb 21, 2013 at 2:53 MiguelMiguel 2,0795 gold badges29 silver badges53 bronze badges 1-
maybe
/\[[0-9]*\]/;
No need for the g flag because you don't want to replace globally. – Jess Commented Feb 21, 2013 at 2:55
2 Answers
Reset to default 9This should work:
var str = '[1] blabh balh blah';
str = str.replace(/\[.*?\]\s?/g, '');
If you have nested brackets regexp might no be the best option though.
Is using regexp a requirement? If not, a simple solution might be:
var myString = '[1] Bob Loblaw is the man';
myString = myString.slice(myString.indexOf(']')+1);
本文标签: regexReplace string between quotquot with javascript using regexpStack Overflow
版权声明:本文标题:regex - Replace string between "[]" with javascript using regexp - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741677232a2391953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论