How to replace hyphen-minus with minus?

494 Views Asked by At

In my test, I need to parseFloat() a string. The issue is that hyphen-minus instantly occurs NaN. Visual Studio Code cannot distinguish hyphen minus from minus so that I am not able to replace on a string. Is there any workaround for this? My code:

Cypress.Commands.add(
  "parseNumber",
  (locator, responseValue) => {
    let parsedText;
    locator()
      .invoke("text")
      .then((text) => {
        parsedText = parseFloat(text.replace("-", "-").replace(" ", "").replace(",", "."));
        expect(parsedText).to.eq(responseValue);
      });
  }
);
1

There are 1 best solutions below

0
wojnarto On

Ok, I did it in that way:

parsedText = parseFloat(
          text
            .replace(/−/, "-")
            .replace(" ", "")
            .replace(/,/, ".")
            .replace(/%/, "")
        );

Resolved, thank you for the answers.