Regular expression on javascript

Hi! Im having troubles trying to understand regex,im currently working on a function that extracts a number between the dollar sign well i have no problem with checking before the number but i do have a problem in terms of checking in the middle,here’s an example : 14$99
function extractNumber(string){
let extract = string.match(/$(\d+.\d+)/)
}
I expected to be " string.match(\d+.$\d+) " but it returns me an error.

Thanks in advance.
pd:Sorry for my bad english ahah

Hello @nesiquatro,

From your query, I understand that you would like to extract numbers from a string.

This should work:

function extractNumber(string) {
    return string.replace(/\D/g, '');
}

The above code would only work for non-decimal numbers, for eg: 14$99 → 1499

if you need it to work with decimal numbers as well, here it is

function extractNumber(string) {
  return parseFloat(string.replace(/[^0-9.]/g, ''));
}

for eg: 144$7.2 → 1447.2

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.