I have this price: $59.95 and I want to get the currency USD $ out of it with the help of RegEx.
This is what I have written so far (I am using Playwright Test and Typescript):
public async getCurrency(): Promise<string | undefined> {
const price = await this.price.textContent();
console.log('PRICE');
console.log(price);
const currency = price?.replace(/\\d \\.\\d /g, '');
console.log('CURRENCY');
console.log(currency);
return currency;
}
But the currency in the above log file is $59.95 instead of $.
The price is as expected - $59.95.
Can you help? What am I doing wrong?
CodePudding user response:
you can change your Regex to...
tip: Use trim to remove whitespace from border
const price "$ 88.88"
const currency = price.replace(/\s*\d*[\.|\,]\d*\s*/, "").trim()
CodePudding user response:
Silly me, sorry guys! I just had to do this (note no double \):
const currency = price?.replace(/\d \.\d /g, '');