'check number equality in Javascript

I have numbers that will be coming from database with this format "+2349091736209" and I want to compare each number with a number I get from user contacts list. The value of phone numbers I get from the contacts is sometimes in this format "0909 173 6209" with spaces, while sometime like this "0909-173-6209". I need help on how to compare the db value with this variants and get if its a match. Thanks.



Solution 1:[1]

Sounds like a good use for regex.

dbNumber.replace(/\D/g, '') === contactsNumber.replace(/\D/g, '')

This takes the string and replaces any non-number information with an empty string. You can find this and more at one of the many Javascript regex playgrounds: https://regexlearn.com/cheatsheet

Solution 2:[2]

phone_number.replace(/,/g, ""); phone_number.replace(/,/g, "-"); gives you the phone number without spaces or -. So you can compare.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abe
Solution 2 Aravind Reddy