开发者

RegEx to match pair of Rectangular Brackets using javascript

Can someone help me with a Javascript regular expression? I need to match pairs of brackets. For example, it should match "[abc123]", "[123abc]" in the following string:

"this is a test [abc123]], another test [[123abc]. This is an left alone closing"

Th开发者_JAVA技巧anks in advance.


If you don't require nested brackets,

// theString = "this is a test [abc123]], another test [[123abc]. 
// This is an left alone closing";
return theString.match(/\[[^\[\]]*\]/g);
// returns ["[abc123]", "[123abc]"]

to extract the contents, see the following example:

var rx = /\[([^\[\]]*)\]/g;
var m, a = [];
while((m = rx.exec(theString)))      
  a.push(m[1]);
return a;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜