If you use the RegExp.exec() method, you can get the necessary information.
var pattern = /\d+\.?\d*|\.\d+/; var match = pattern.exec("the number is 7.5!"); var start = match.index; var text = match[0]; var end = start + text.length;
/\d+\.?\d*|\.\d+/ equivalent to new RegExp("\\d+\\.?|\\.\\d+") . The literal syntax retains some backslashes.
source share