username = "Npasandarshana@gmail.com"
username =“Npasandarshana@gmail.com”
password = "S@pasan123"
密码=“S @ pasan123”
According to above example, string pattern "pasan" contains in the username.
根据上面的例子,字符串模式“pasan”包含在用户名中。
I want to check whether all password or part of password contains in username for form validating before registration
我想在注册前检查所有密码或密码的一部分是否包含用于表单验证的用户名
i used javascript search function as below
我用javascript搜索功能如下
password.search(username) == '-1'
but It searches whole password string in the username and it is not accurate.
但它在用户名中搜索整个密码字符串并且不准确。
Can I use a regex pattern to check some scenario like above in javascript?
我可以使用正则表达式模式来检查javascript中的上述情况吗?
2 个解决方案
#1
2
Match the string with a-z
then .Array#filter
the length above 4 and username
contains
将字符串与a-z匹配,然后.Array#过滤长度大于4且用户名包含
function check(use,pwd){
return pwd.match(/[a-z]+/ig).filter(a=> a.length > 4 && use.includes(a)).length > 0? true:false;
}
console.log(check("Npasandarshana@gmail.com","S@pasan123"))//true
console.log(check("Npasandarshana@gmail.com","S@pasysan123"))//false
#2
0
RegExp
var string = "foo",
expr = /oo/;
if(expr.test(string)){
console.log('Yes');
}else{
console.log('No')
}
Match
var string = "foo",
expr = "/oo/";
if(string.match(expr)){
console.log('Yes');
}else{
console.log('No')
}
(ES6) includes
var string = "foo",
substring = "oo";
if(string.includes(substring)){
console.log('Yes');
}else{
console.log('No')
}
Also refer: How to check whether a string contains a substring in JavaScript?
另请参阅:如何检查字符串是否包含JavaScript中的子字符串?
#1
2
Match the string with a-z
then .Array#filter
the length above 4 and username
contains
将字符串与a-z匹配,然后.Array#过滤长度大于4且用户名包含
function check(use,pwd){
return pwd.match(/[a-z]+/ig).filter(a=> a.length > 4 && use.includes(a)).length > 0? true:false;
}
console.log(check("Npasandarshana@gmail.com","S@pasan123"))//true
console.log(check("Npasandarshana@gmail.com","S@pasysan123"))//false
#2
0
RegExp
var string = "foo",
expr = /oo/;
if(expr.test(string)){
console.log('Yes');
}else{
console.log('No')
}
Match
var string = "foo",
expr = "/oo/";
if(string.match(expr)){
console.log('Yes');
}else{
console.log('No')
}
(ES6) includes
var string = "foo",
substring = "oo";
if(string.includes(substring)){
console.log('Yes');
}else{
console.log('No')
}
Also refer: How to check whether a string contains a substring in JavaScript?
另请参阅:如何检查字符串是否包含JavaScript中的子字符串?