jeudi 26 mars 2015

A simpler way to capture multiple variables in javascript regexps


Comming from the perl/python world I was wondering if there is a simpler way to filter out multiple captured variables from regexp in javascript:



#!/usr/bin/env node
var data=[
"DATE: Feb 26,2015",
"hello this should not match"
];

for(var i=0; i<data.length; i++) {
var re = new RegExp('^DATE:\\s(.*),(.*)$');
if(data[i].match(re)) {
//match correctly, but how to get hold of the $1 and $2 ?
}
if(re.exec(data[i])) {
//match correctly, how to get hold of the $1 and $2 ?
}

var ret = '';
if(data[i].match(re) && (ret = data[i].replace(re,'$1|$2'))) {
console.log("line matched:" + data[i]);
console.log("return string:" + ret);
ret = ret.split(/\|/g);
if (typeof ret !== 'undefined') {
console.log("date:" + ret[0], "\nyear:" + ret[1]);
}
else {
console.log("match but unable to parse capturing parentheses");
}
}
}


The last condition works, but you need a temp var and split it, and you need to have a test in front because the replace works on everything.


Output is:



$ ./reg1.js
line matched:DATE: Feb 26,2015
return string:Feb 26|2015
date:Feb 26
year:2015


If I look up: mosdev regexp it says on (x):



The matched substring can be recalled from the resulting array's elements 1, ..., [n] or from the predefined RegExp object's properties $1, ..., $9.



How do I get hold of the RegExp objects' $1 and $2?


Thanks





Aucun commentaire:

Enregistrer un commentaire