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
Add a ment  | 

2 Answers 2

Reset to default 9

This 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