How to remove "$" and "." with regex

2.8k views Asked by At

i'm currently web scraping an ecommerce website which i am choosing the prices of the products. The price follows this format:

$4.500

And i currently have this in the regExr of the web scraping software:

[0-9]+\.[0-9]+

I'm currently just looking how can i remove the dollar sign and the dot? To leave it like this

4500

What i understand from regex is that i can match the $ and . character. So i tried it on regex101.com this:

\$[0-9]+.[0-9]+

What is the regex so i can match certain characters and delete them?

2

There are 2 answers

0
Dumidu Udayanga On
String s = "$4500";
s = s.replace("$", "");
s = s.replace(".", "");
0
Nick Howard On

In JavaScript, this can be done in the following way (if you still want to use regex):

const [dollarSymbol, ...values] = /\$([0-9]+)\.([0-9]+)/.exec('$4.500');
const result = values.join('');

or a shorter way would be:

'$4.500'.replace(/[$.]/g, '');