Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut string after comma and letters befor numbers

Tags:

javascript

I have a string

var numb = "R$ 2000,15"

I would like to cut two last numbers and comma,and R$ with space, to get result => 2000.

I tried with regex: (?!\d{1,5}),(?:\d{2}) and it takes result: R$ 2000. So now I would like to remove R$ with space.

Any help?

like image 338
anna Avatar asked Nov 07 '22 12:11

anna


1 Answers

Try this regex, it should do the trick:

/^R\$\s(\d+)((\,\d{2})?)$/

To use it, you can replace like this:

let result = myNumber.replace(/^R\$\s(\d+)((\,\d{2})?)$/, "$1");

Note that each group between parentheses will be captured by your regex for replacement, so if you want the set of numbers before the comma you should use the corresponding group (in this case, 1). Also note that you should not put your regex between quotes.

like image 81
Ayrton Avatar answered Nov 15 '22 05:11

Ayrton