Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Suppose I want to know whether a string contains 5 or more continuous consecutive numbers.

var a = "ac39270982"; // False
var a = "000223344998"; // False
var a = "512345jj7"; // True - it contains 12345
var a = "aa456780"; // True - it contains 45678

Is there a RegEx available to accomplish this? Would it also be able to work in the following situation?

var a = "5111213141587"; // True

This should be true because it contains 11,12,13,14,15.

I'm not sure if it is possible to check the provided examples (single-digit, double-digit numbers) as well as larger numbers (triple-digit, etc.).

like image 653
Firos Shamsudeen Avatar asked Jun 19 '15 00:06

Firos Shamsudeen


1 Answers

I took the time to make a 100% Javascript approach to your question. I made it to simply parse each character in the string and do integer only comparison. This works not only for five consecutive integers, but it works for checking for tenths as well (10's, 20's, etc). You can also increase/decrease the number of comparisons if you wish.

A fair warning: despite this method being potentially scalable if coded to look for all kinds of numeric sizes, you'd still be bound by computing power and number of comparisons. That is why I only provided the code for single digits and tenths, I leave it open to you/the community to decide how to expand from here.

jsFiddle

If you happen to need more details about how it works then let me know, I can further clarify its inner workings.

var str = "1111122asdgas222*&^%121314151617bdjfjahdi234bdce56789";
var consecutive = 5; // Number of comparisons

// Single digits
alert("It is " + consecutiveDigits(str, consecutive) + " that " + str + " contains " + consecutive + " consecutive digits.");
// Tenths digits
alert("It is " + consecutiveDigits(str, consecutive) + " that " + str + " contains " + consecutive + " consecutive tenths.");

function consecutiveDigits(str, consecutive){
    var curr,
        prev,
        count = 0;
    for(var i = 0; i < 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 i = 0; i < 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);
}
like image 133
AGE Avatar answered Sep 28 '22 08:09

AGE