gpt4 book ai didi

Javascript检查字符串中的三个升序字母和数字

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:12 25 4
gpt4 key购买 nike

我想弄清楚如何检查字符串中是否包含三个升序字母和/或数字。例如,如果一个字符串有“manabcrt”然后有“abc”或“caSTLe567”,字符串中有“567”或“caSTLexyzand789”它有“xyz”和789 ...所以我想检查一下.我找到这个fiddle但它对重复的字母有效。

这是来自 fiddle 的代码:

var inputs = [
'helloworld',
'hellloworld',
'aaabc',
'abcddd',
'bbabba'];

var violators = [];
inputs.forEach(function(input) {
if (/(.)\1\1/.test(input)) {
violators.push(input);
}});
alert("VIOLATORS:\n" + violators.join('\n'));

最佳答案

您可以通过计算升序对来检查值。

var array = ['manabcrt', 'castle567', , 'castlexyzand789', 'helloworld', 'hellloworld', 'aaabc', 'abcddd', 'bbabba'],
check = array.filter(s => {
var v = 1;
return [...s].some((c, i, a) => {
v *= parseInt(c, 36) + 1 === parseInt(a[i + 1], 36);
return ++v === 3;
});
});

console.log(check);

对案例 '90' 和不包括 '9a' 的一些调整

var array = ['zab', '901', '9ab', 'manabcrt', 'castle567', , 'castlexyzand789', 'helloworld', 'hellloworld', 'aaabc', 'abcddd', 'bbabba'],
check = array.filter(s => {
var v = 1;
return [...s].some((c, i, a) => {
var l = parseInt(c, 36),
r = parseInt(a[i + 1], 36);

v *= l + 1 === r && l !== 9 && r !== 10 || l === 9 && r === 0 || l === 35 && r === 10;
return ++v === 3;
});
});

console.log(check);

关于Javascript检查字符串中的三个升序字母和数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48545590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com