javascript - RegEx - Value inside double square brackets - Stack Overflow

I'm trying to extract a value inside Double Square brackets: [[x]], whilst ignoring everything els

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 0
Add a ment  | 

6 Answers 6

Reset to default 3

Given:

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, &nbsp; <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

相关推荐

  • javascript - RegEx - Value inside double square brackets - Stack Overflow

    I'm trying to extract a value inside Double Square brackets: [[x]], whilst ignoring everything els

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信