2016-04-30 6 views
5

は、私は私が/\[\[(\S+)\]\]/を建てuser.system.first_nameuser.custom.luid正規表現を使用して文字間のデータを抽出しますか?

一致させたい[[user.system.first_name]][[user.custom.luid]] blah blah

のような文字列の何かを持っているが、それはuser.system.first_name]][[user.custom.luidに一致しています。

私が間違っているのは何ですか?

+0

'/ \ [\ [(\ S +?)をステップ44で完了することが判明\ ] \]/' –

+1

ポー[大括弧で囲まれたテキストを抽出するための正規表現]の重複の可能性(http://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) –

答えて

3

として、非貪欲作りは、それが非欲張りできるだけ少ない入力文字に一致するように?を使用してください。

\[\[([^\]]*)\]\] 

Regex101 Demo

1

になること。

[^[]+?(?=]]) 

Regular expression visualization

Debuggex Demo

Regex101

var s = "[[user.system.first_name]][[user.custom.luid]]", 
 
    m = s.match(/[^[]+?(?=]])/g); 
 
document.write("<pre>" + JSON.stringify(m,null,2) + "</pre>") ;

1

私は/[^[]+?(?=]])/gは1つの速い正規表現だと思う:あなたは2つの別々の一致が使用が必要な場合は、あなたの正規表現は、/\[\[(\S+?)\]\]/

var str = '[[user.system.first_name]][[user.custom.luid]] blah blah' 
 
var reg = /\[\[(\S+?)\]\]/g, 
 
    match, res = []; 
 

 
while (match = reg.exec(str)) 
 
    res.push(match[1]); 
 

 
document.write('<pre>' + JSON.stringify(res, null, 3) + '</pre>');

関連する問題