My JavaScript code uses a regular expression, myRegexp
, to match numbers in a string:
var myRegexp = new RegExp('[0-9]+');
The code then extracts numbers from the string and returns an array:
var string = '123:456';
var nums = [];
while(myRegexp.test(string)) {
nums.push(myRegexp.exec(string)[0]);
string = string.replace(myRegexp, '');
}
This should give us an array with two elements: "123" and "456".
However, using RegExp.lastMatch
and RegExp.rightContext
is not recommended as they are deprecated and non-standard. How can I achieve the same functionality using standard JavaScript API?
Thank you.