regex - How to find whether specific number of continuous consecutive numbers are contains in a string using javascript? -


suppose want know whether string contains 5 or more continuous consecutive numbers.

var = "ac39270982"; // false var = "000223344998"; // false var = "512345jj7"; // true - contains 12345 var = "aa456780"; // true - contains 45678 

is there regex available accomplish this? able work in following situation?

var = "5111213141587"; // true 

this should true because contains 11,12,13,14,15.

i'm not sure if possible check provided examples (single-digit, double-digit numbers) larger numbers (triple-digit, etc.).

i took time make 100% javascript approach question. made parse each character in string , integer comparison. works not 5 consecutive integers, works checking tenths (10's, 20's, etc). can increase/decrease number of comparisons if wish.

a fair warning: despite method being potentially scalable if coded kinds of numeric sizes, you'd still bound computing power , number of comparisons. why provided code single digits , tenths, leave open you/the community decide how expand here.

jsfiddle

if happen need more details how works let me know, can further clarify inner workings.

var str = "1111122asdgas222*&^%121314151617bdjfjahdi234bdce56789"; var consecutive = 5; // number of comparisons  // single digits alert("it " + consecutivedigits(str, consecutive) + " " + str + " contains " + consecutive + " consecutive digits."); // tenths digits alert("it " + consecutivedigits(str, consecutive) + " " + str + " contains " + consecutive + " consecutive tenths.");  function consecutivedigits(str, consecutive){     var curr,         prev,         count = 0;     for(var = 0; < str.length; ++i) {         curr = parseint(str.split('')[i]);         if(isnumeric(curr)) {             if(count === 0){                 ++count;             }             else if(prev + 1 === curr){                 ++count;                 if(count === consecutive){                     return true;                 }             }             prev = curr;         }     }     return false; }  function consecutivetenths(str, consecutive, iterations){     var curr,         prev,         curr_tenth = 0,         prev_tenth = 0,         count = 0,         count_tenth = 0;      for(var = 0; < str.length; ++i) {         curr = parseint(str.split('')[i]);         if(isnumeric(curr)) {             ++count;             if(count === iterations){                 curr_digit = (prev * 10) + curr;                 alert(count_digit + " " + curr_digit + " " + prev_tenth);                 if(count_digit === 0){                     ++count_digit;                 }                 else if(curr_tenth === (prev_tenth + 1)){                     ++count_digit;                     if(count_digit === consecutive){                         return true;                     }                 }                 prev_digit = curr_digit;                 count = 0;             }             else {                 prev = curr;             }         }         else {             count = 0;         }     } }   function isnumeric(n) {     return !isnan(parsefloat(n)) && isfinite(n); } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -