greetings for all you Jedi that can properly work with JS, I unfortunately cannot.
I want to iterate in all matches of the respective string ' m10 m20 m30 xm40', except the xm40, and extract the numbers, 10, 20, 30:
' m10 m20 m30 xm40'.match(/\s+m(\d+)/g)
' m10 m20 m30 xm40'.match(/\s+m(\d+)/g)
(3) [" m10", " m20", " m30"]
Use RegExp.exec()
function:
const regex = /\sm(\d+)/g;
const str = ' m10 m20 m30 xm40';
let result = [];
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
result.push(+m[1]);
}
console.log(result);