javascript - 检查文本是否是系列的子集
问题描述
我想根据一个系列检查一个字符串(关于顺序)
- 请参阅下面的另一种方法(字符串解决方案)
正则表达式解决方案(潜在)
r = /[(qw|az)ert(y|z)uiop(é|å|ü)]{5}/g
str = "ertyu"
str2 = "rtrrr"
r.test(str) // true
r.test(str2) // true
str2 应该返回 false 因为它不遵守顺序,但由于正则表达式被包装在数组中,我认为这种方法从一开始就是错误的。
字符串解决方案
arr = [
"qwertyuiopé",
"qwertyuiopå",
"qwertyuiopü",
"qwertzuiopé",
"qwertzuiopå",
"qwertzuiopü",
"azertyuiopé",
"azertyuiopå",
"azertyuiopü",
"azertzuiopé",
"azertzuiopå",
"azertzuiopü",
]
str = "ertyu"
str2 = "yrwqp"
function test(s) {
for (const a of arr) {
if (a.indexOf(str) >= 0) return true
}
return false
}
test(str) // true
test(str2) // false
字符串版本有效,但它又丑又大
正则表达式有没有办法让它工作?
解决方案
最后,我认为我想要实现的目标是不可能的,我最终得到了一系列不同系列(键盘琐碎系列)和一个检查密码是否是系列序列的函数
const trivialseries = [
// swedish, german, french, english, spanish, italian - keyboard
"1234567890",
"qwertyuiopå", // se|en
"asdfghjklöä",
"zxcvbnm",
"qwertzuiopü", // de
"yxcvbnm",
"azertyuiop", // fe
"qsdfghjklmù",
"wxcvbn",
"asdfghjklñ", // sp
"qwertyuiopé", // it
"asdfghjklòàù",
];
const MAX = 5;
function subsequence(serie, str) {
for (let i = 0; i < str.length - MAX + 1; i++) {
const index = serie.indexOf(str[i]);
if (index >= 0) {
let found = 1;
for (let j = i + 1; j < str.length; j++) {
if (serie[index + found] === str[j]) found++;
else break;
if (found === MAX) return true;
}
}
}
return false;
}
function isTrivial(password) {
for (let ts of trivialseries) {
if (subsequence(ts, password)) return true;
const reverse = ts.split("").reverse().join("");
if (subsequence(reverse, password)) return true;
}
return false;
}
console.log(isTrivial("e927ncsmnbvcdkeloD€%s567jhdoewpm")); // true "mnbvc" is reverse form of "cvbnm"
推荐阅读
- ios - 如何以编程方式在 iOS 设置应用程序中打开“首选语言”页面?
- reactjs - react-chartjs-2 (chart.js) 雷达 - 删除值?
- javascript - JS:为什么在继承另一个原型后需要重新设置构造函数?
- c# - 在取消和处理 CancellationTokenSource 之间存在延迟是否可以保证 IsCancellationRequested 将设置为 true?
- javascript - 找不到模块 axios - 与打字稿的兼容性问题
- javascript - Discord js在嵌入中使用消息ID
- c++ - 字符串未定向到输出文件(C++)
- python-3.x - 为什么使用 "+=" 附加到 List[str] 会导致意外的换行符,而 "c = c + a" 会导致 c 为空?
- java - 使用java中的方法交换单链表中的两个节点
- python - 如何对文件中的int进行排序