Regular Expression Match

Regular Expression Match
0

#1

Hello, does anyone know why my console is showing 2 copies of the regex that I’m matching? My regex is such that it finds numbers with or without a decimal occurring after a math operator at the end of the checked string.

Instead of my anticipated console-log of 30.5, I get 30.5, 30.5.

let myString = ".323+30.5";
let myRegex = /(\d*\.?\d+)$/; 
let getMatch = myString.match(myRegex);
console.log(getMatch)

#2

It’s because you have a captured group match (), javascript will return the entire matched string as the first argument, and each captured group as the following elements.

If you remove the group, then it will show only the entire match itself.

let myRegex = /\d*\.?\d+$/;

#3

Wow, thanks for the Rapid Response!