开发者

RegEx to match pair of Rectangular Brackets using javascript

开发者 https://www.devze.com 2023-01-17 03:54 出处:网络
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:

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

精彩评论

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