Hi I need to get a string inside 2 pair of square brackets in javascript using regular expressions.
here is my string [[12]],23,asd
So far what I tried is using this pattern ' \\[\\[[\\d]+\\]\\]
'
and I need to get the value 12 using regular expressions
You can use the following regex,
\[\[(\d+)\]\]
This will extract 12
from [[12]],23,asd
It uses capture groups concept
\[\[(\d+)\]\]
Try this.Grab the capture or group 1.See demo.
var re = /\[\[(\d+)\]\]/gs;
var str = '[[12]],23,asd';
var m;
while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}
You can capture the digits using groups
"[12]],23,asd".match(/\[\[(\d+)\]\]/)[1]
=> "12"
这是您可以使用的正则表达式,捕获组分别获得$1
和$2
,分别为 12 和 43
\[\[(\d+)\]\]\S+\[\[(\d+)\]\]
If you need to get 12 you can just use what you mentioned with a capturing group \\[\\[(\\d+)\\]\\]
var myRegexp= /\[\[(\d+)\]\]/;
var myString='[[12]],23,asd';
var match = myRegexp.exec(myString);
console.log(match[1]); // will have 12
I've only done it with 2 regExps, haven't found the way to do it with one:
var matches = '[[12]],23,asd'.match(/\[{2}(\d+)\]{2}/ig),
intStr = matches[0].match(/\d+/ig);
console.log(intStr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.