Regular Expression to match 3 or more Consecutive Sequential Characters and Consecutive Identical Characters

前端 未结 15 2227
北荒
北荒 2020-11-28 11:41

I need regular expressions to match the below cases.

  1. 3 or more consecutive sequential characters/numbers; e.g. 123, abc, 789, pqr, etc.
  2. 3 or more cons
15条回答
  •  失恋的感觉
    2020-11-28 12:17

    Try this for the first question.

    returns true if it finds 3 consecutive numbers or alphabets in the arg

    function check(val){
        for (i = 0; i <= val.length - 3; i++) {
          var s1 = val.charCodeAt(i);
          var s2 = val.charCodeAt(i + 1);
          var s3 = val.charCodeAt(i + 2);
          if (Math.abs(s1 - s2) === 1 && s1 - s2 === s2 - s3) {
            return true;
          }
        }
    
        return false;
    }
    
    console.log(check('Sh1ak@ki1r@100'));
    

提交回复
热议问题