javascript - Replace query string returns only first array value using Regex? -
my url having parameter value follows below:
nr=and(or(abc:def),or(ghi:jkl),or(mno:pqr)...)
used below regex look extract above query string returns first array value ex. getting abc , def value in array.
or\(([^:]*):([^)]*)\)
i wanted extract values 2 separate array values abc,ghi,mno , def,jkl,pqr...
plz find code below:
var getnrvalue = 'and(or(analyzed:abc),or(compounds:def),or(chemical:mno))'; var regex = /or\(([^:]*):([^)]*)\)/gm; var s = regex.exec(getnrvalue); console.log(s);
any help on this?
you can utilize regex:
([^():]+):([^():]+)
in regex demo, right pane shows capture groups. there live js demo.
use code create arrays (see output of live js demo):
var array1 = []; var array2 = []; var string = 'nr=and(or(abc:def),or(ghi:jkl),or(mno:pqr)...)' var string = 'nr=and(or(abc:def),or(ghi:jkl),or(mno:pqr)...)' var myregex = /([^():]+):([^():]+)/g; var thematch = myregex.exec(string); while (thematch != null) { // add together array of captures array1.push(thematch[1]); array2.push(thematch[2]); document.write("left side: ",thematch[1],"<br />"); document.write("right side: ",thematch[2],"<br />"); // match next 1 thematch = myregex.exec(string); }
explanation:
([^():]+)
captures grouping 1 characters not parentheses ()
or colons :
:
([^():]+)
captures grouping 2 characters not parentheses ()
or colons :
the code retrieves grouping 1 , grouping 2 matches , pushes them onto 2 arrays let me know if have questions. :)
javascript regex
No comments:
Post a Comment