Given myString 6 cyl, 3.2L (3197cc) engine
, the following needs to be extracted: 6 cyl
and 3197cc
.
This code is failing and not sure how to fix it.
const rgx = /(\d cyl).*(\d{2,4}?cc)/g; //defined outside a loop
//inside the loop we have myString changes every run.
let match = rgx.exec(myString);
console.log(match[1]); // => 6 cyl
console.log(match[2]); // => 97cc <--------- suppose to be 3197cc
Then the next loop around, the whole thing fail to match saying
"Cannot read property '1' of null".
What am I doing wrong?