Removing Characters from a String (JS)

Asked

Viewed 4,627 times

-1

I have a question, I need to remove a specific character of a string in my input, I used a mask to convert it into Real, but as it is with comma and point my js script is not recognizing there need to remove the point and comma when running my Function in js .. Ex:

10,000.00

Remove the , .

How can I do that ? Remembering that the numbers I’m getting as string ...

2 answers

1


You can use the function replace along with a regular expression.

Example:

const str = '10,000.00'
const strNum = str.replace(/[^0-9]/g, '')
console.log(strNum) // 1000000

0

Just retrieve your string, and remove all comma occurrences, and all point occurrences, replacing with the empty string.

It is possible to do this using the following code:

const valorstr = "100,101,000.00";
const sempontoevirgula = valorstr.replace(/,/g, "").replace(/\./g, "");

In this example, we use the /g to replace all occurrences of the comma.

  • Thank you very much personally I used the examples of you let mensal = (mensal1.value.replace(/[^0-9]/g, ''))
 let resultSoma1 = (result2.value.replace(/[^0-9]/g, '')) Worked Perfectly thanks ! to everyone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.