我需要验证节点J中电话号码的前四位数字为“ 5678”。我该怎么办?我应该使用哪个验证程序库

我需要验证nodeJs中电话号码的前四位数字应为“ 5678”,我该怎么做。我应该使用哪个验证程序库

sunshengxia 回答:我需要验证节点J中电话号码的前四位数字为“ 5678”。我该怎么办?我应该使用哪个验证程序库

在我看来,最好的方法是仅在需要时才依赖外部库。

对于您而言,您可以使用简单的正则表达式检查电话号码是否以“ 5678”开头:

main

const validate_phone = /^5678/ var phone_number = '5678122535' console.log(validate_phone.test(phone_number)) // true var phone_number = '1567812253' console.log(validate_phone.test(phone_number)) // false 代表matches beginning of input

^代表executes a search for a match between a regular expression and a specified string

,

您可以使用子字符串函数,就复杂性而言,它比正则表达式更好。

const string = "56789012345" ;
string.substring(0,4)==="5678" ? true : false ;
本文链接:https://www.f2er.com/3139310.html

大家都在问