1

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

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.

4

2 に答える 2

8

これはうまくいくはずです:

var str = '[1] blabh balh blah';
str = str.replace(/\[.*?\]\s?/g, '');

ただし、入れ子になったブラケットがある場合、正規表現は最適なオプションではない可能性があります。

于 2013-02-21T02:56:06.223 に答える
1

正規表現を使用する必要がありますか?そうでない場合、簡単な解決策は次のようになります。

var myString = '[1] Bob Loblaw is the man';
myString = myString.slice(myString.indexOf(']')+1);
于 2013-02-21T03:09:19.720 に答える