0

Here is my code value = str.match(/$(\d+)/);

For example 1: HK$999 Display Result = 999

For example 2: HK$1,999.20 Display result = 1 (I would not want this result)

How can show the example 2 = 1999

Thank you all ^_^

SAM941
  • 9
  • 1
  • 1
    try regex from https://stackoverflow.com/questions/10003683/how-can-i-extract-a-number-from-a-string-in-javascript – Devsi Odedra May 25 '22 at 03:16

1 Answers1

1

This code should work fine first remove all the unnecessary comas from the string then select all the numbers before "."

var ragex = /(\d+)/g;

let str = "HR999";
str = "HK$1,999.20"
str = str.replace(/(,)/, '')

let res = str.match(ragex)[0];

the result will be at first index in the array

if you want numbers including.

use this ragex

var ragex = /\d+.\d+/g;
Himanshu Jangid
  • 369
  • 4
  • 15
  • Numbers with locale separator can be more complicated than just removing commas. [https://stackoverflow.com/a/42000120/10317684](https://stackoverflow.com/a/42000120/10317684) – Ricky Mo May 25 '22 at 03:43