Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
449 views
in Technique[技术] by (71.8m points)

javascript - check if text is a subset of a serie

I want to check a string against a serie (with respect to order)
-- see bellow for another approach (string solution)

Regex solution (potential)

r = /[(qw|az)ert(y|z)uiop(é|?|ü)]{5}/g
str = "ertyu"
str2 = "rtrrr"

r.test(str)  // true 
r.test(str2) // true

The str2 should return false as it does not respect the order, but since the regex is wrapped in array I assume this approach is wrong from the start.

String solution

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

The string version works but its ugly and big

Is there a way with regex to get this working?

question from:https://stackoverflow.com/questions/65936566/check-if-text-is-a-subset-of-a-serie

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In the end I don't think what I want to achieve would be possible, I ended up with an array of different series (keyboard trivial series) and a function that checks if a password is a sequence of the series

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...