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;
精彩评论