2016-04-30 12 views
5

Ho un qualcosa di stringa come [[user.system.first_name]][[user.custom.luid]] blah blahEstrarre i dati tra i caratteri usando regex?

voglio abbinare user.system.first_name e user.custom.luid

ho costruito /\[\[(\S+)\]\]/ ma è la corrispondenza user.system.first_name]][[user.custom.luid.

Qualche idea su dove sto sbagliando?

+0

'/ \ [\ [(\ S +?) \ ] \]/' –

+1

Po duplicato corretto di [Espressione regolare per estrarre il testo tra parentesi quadre] (http://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-tra between-square-brackets) –

risposta

3

renderlo non avido utilizzando ? per abbinare il minor numero di caratteri di input possibili. Che il vostro regex sarà /\[\[(\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>');

1

Se avete bisogno di 2 partite separato uso:

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

Regex101 Demo

1

Credo /[^[]+?(?=]])/g è una regex veloce. Si scopre che per essere completato in 44 passi

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

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>") ;