[英]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.我有来自数据库的号码,格式为“+2349091736209”,我想将每个号码与我从用户联系人列表中获得的号码进行比较。 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".
我从联系人那里得到的电话号码的值有时是这种带空格的“0909 173 6209”格式,而有时是这样的“0909-173-6209”。 I need help on how to compare the db value with this variants and get if its a match.
我需要有关如何将 db 值与此变体进行比较并确定其是否匹配的帮助。 Thanks.
谢谢。
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
您可以在许多 Javascript 正则表达式游乐场之一找到这个和更多: https://regexlearn.com/cheatsheet
phone_number.replace(/,/g, ""); phone_number.replace(/,/g, ""); phone_number.replace(/,/g, "-");
phone_number.replace(/,/g, "-"); gives you the phone number without spaces or -.
给你没有空格或 - 的电话号码。 So you can compare.
所以你可以比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.