I'm trying to extract a value inside Double Square brackets: [[x]]
, whilst ignoring everything else in single square brackets: [y]
.
NOTE: This RegEx is to be used in JavaScript, which as far as I understand doesn't support Looking behind
For example, take these 3 strings
[[32]] [Test] Lorem Ipsum
[[16]] Lorem Ipsum
[[2]] Test [BUG]
I want to extract: 32, 16, 2
This is what I've tried
Test 1:
\[([0-9]+)\]
Will only return the value including the inner square brackets, e.g. [32], [16], [2]. I could then just do another RegEx on the result, but I'd like to know how to do it once.
Test 2:
\[.*?\]]
Will return the value, with the double square brackets, e.g. [[32]], [[16]], [[2]]
I'm trying to extract a value inside Double Square brackets: [[x]]
, whilst ignoring everything else in single square brackets: [y]
.
NOTE: This RegEx is to be used in JavaScript, which as far as I understand doesn't support Looking behind
For example, take these 3 strings
[[32]] [Test] Lorem Ipsum
[[16]] Lorem Ipsum
[[2]] Test [BUG]
I want to extract: 32, 16, 2
This is what I've tried
Test 1:
\[([0-9]+)\]
http://www.regexr./39sm3
Will only return the value including the inner square brackets, e.g. [32], [16], [2]. I could then just do another RegEx on the result, but I'd like to know how to do it once.
Test 2:
\[.*?\]]
http://www.regexr./39sm0
Will return the value, with the double square brackets, e.g. [[32]], [[16]], [[2]]
Share Improve this question edited Nov 11, 2014 at 5:17 Pete asked Nov 11, 2014 at 3:13 PetePete 9051 gold badge6 silver badges13 bronze badges 06 Answers
Reset to default 3Given:
var s = '[[32]] [Test] Lorem Ipsum [[16]] Lorem Ipsum [[2]] Test [BUG]';
You can use a look–ahead that matches one or more digits followed by ']]' but doesn't include the ']]' in the match:
s.match(/\d+(?=\]\])/g) // ["32", "16", "2"]
Or, if lookahead isn't available, you can use:
s.match(/\[\[(\d+)\]\]/g).map(function(v){return v.replace(/\[+|\]+/g,'');})
though if map is available then probably look–ahead is too. If the input is multi–line, you may need the m flag also.
You can use capturing groups to match substrings within patterns.
In JavaScript (the parentheses define the capturing group here):
var pattern = /\[\[([0-9]+)\]\]/g;
var string = "[[1]] [2] [[3]] [4] [[five]]";
var numbers = [];
while (match = pattern.exec(string)) {
numbers.push(match[1]); //Get capturing group 1. This would be e.g. "1"
// NOTE: match[0] contains the entire match. e.g. "[[1]]"
}
console.log(numbers)
Console output: ["1", "3"]
See http://www.regular-expressions.info/refcapture.html for more info on capturing groups.
How about
\[{2}(\d+)\]{2}
Telling Regex you want {2}
[
then some \d
digits then another {2}
]
Did you try /\[\[([0-9]+)\]\]/
? Assuming of course you only expect digits within the double square brackets.
var exp = /\[\[([0-9]+)\]\]/;
var number = exp.exec("[[293]] Test line")[1]
number
should return '293'
.
let str = 'test [[1]], test [[2]], xyz, abc, <p>Hi</p> test [[3]].'
let result = str.match(/\[\[(\d+)\]\]/g).map((item) => {
return parseInt(item.replace(/[\[\]]/g, ''))
})
console.log(result)
Another way of doing it with pure regex would be to use lookarounds.
(?<=\[\[)[0-9]+(?=\]\])
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745307516a4621801.html
评论列表(0条)