Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6954777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:39:00+00:00 2026-05-27T14:39:00+00:00

I’m building a node.js application and storing a six-digit base36 representation of a unix

  • 0

I’m building a node.js application and storing a six-digit base36 representation of a unix timestamp (in seconds) as the first part of an _id in Mongodb. A typical _id looks like this:

"_id" : "lwhlzy/czwszasfgr/a4d18976c1/f835caa1c3/184d06b47f"

Several pieces of data are concatenated, including the timestamp followed by a series of hashed data to form both a GUID and a “materialized path“

Later queries will select records based on a time range, followed by the path to get events that happened during that period for that particular path. These queries will rely on rooted regular expressions, so I need a regex that can find a range of base36 numbers:

This is the code I have so far (a test to run via node and yes it is hard-coded to six digits. The seventh digit wont be needed until Dec 23rd 2038.)

var base36 = "0123456789abcdefghijklmnopqrstuvwxyz";

// determine how many left-most characters from & to have in common
// this function works nicely, no problems here
var getOverlap = function (from, to) {
    regex = '';
    count = to.length;

    for (i in to) {
        regex += (i>0?'|':'')+'('+to.slice(0,count)+')';
        count--;
    }

    result = from.match(RegExp(regex,"ig"));
    return result[0];
};

var from = "lec0s0"; 
var to = "lwhvqg"; // generated from: parseInt(Date.now()/1000,10).toString(36)

var overlap = getOverlap(from,to);

console.log(from);
console.log(to);

var regex = overlap;
var i = overlap.length;
// start immediately after the left-most common characters and append the rest of the regex
while (i<6) {
    regex += "[";

    if (from[i] < to[i]) {
        regex += base36.slice(base36.indexOf(from[i]), base36.indexOf(to[i])+1);
    } else {
        regex += base36.slice(base36.indexOf(from[i])) + base36.slice(0, base36.indexOf(to[i])+1);
    }

    regex += "]";
    i++;
}

console.log(regex);
process.exit();

Which will output something like this:

l[efghijklmnopqrstuvw][cdefgh][0123456789abcdefghijklmnopqrstuv][stuvwxyz0123456789abcdefghijklmnopq][0123456789abcdefg]

After studying this I realized there are two main issues with this: 1) its not quite right for a true range (it would skip huge chunks of records) and 2) Id rather have character ranges like [e-w] instead of every character explicitly stated though it still works.

For input from="lec0s0" and to="lwhvqg" I realize Im missing a large part of this regex. For example, the code above only allows the 3rd character a range from c-h, but that position will need to reach “z” before the 2nd character can increment. I’ve determined that I actually need a regex that looks more like this:

l[e-v][0-9a-z][0-9a-z][0-9a-z][0-9a-z]|l[e-w][c-g][0-9a-z][0-9a-z][0-9a-z]|l[e-w][c-h][0-9a-u][0-9a-z][0-9a-z]|l[e-w][c-h][0-9a-v][0-9a-o][0-9a-z]|l[e-w][c-h][0-9a-v][0-9a-q][0-9a-g]

So my question is: am I right to conclude the regex needs to look like the latter above? And if so, how might I modify the code to generate it?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:39:01+00:00Added an answer on May 27, 2026 at 2:39 pm

    Your current pattern will match from le0000 and up, you actually wish to match:

    lec0s[0-9a-z]|lec0[t-z][0-9a-z]{1}|lec[1-9a-z][0-9a-z]{2}|le[d-z][0-9a-z]{3}|l[f-v][0-9a-z]{4}|lw[0-9a-g][0-9a-z]{3}|lwh[0-9a-u][0-9a-z]{2}|lwhv[0-9a-p][0-9a-z]{1}|lwhvq[0-9a-g]
    

    The following function should give you the regex you need:

    function getRegex(from,to) {
        var base36 = '0123456789abcdefghijklmnopqrstuvwxyz',
            getRange = function(f,t) {
                if(f == t) {
                    return f;
                }
                if(base36.indexOf(f) >= base36.indexOf(t)) {
                    return t;
                } 
                if(t <= '9' || f >= 'a'){
                    return '[' +f+'-'+t+']';
                }
                return '[' +f+(f<'9'?'-9':'')+(t>'a'?'a-':'')+t+']';    
            },
            from = from.split(''),
            to = to.split(''),
            prefix='', 
            regex=[], 
            tmp,i,l;
    
        for(i=0,l=from.length;i<l;i++) {
            if(from[i]!=to[i]) {
                break;
            }
            prefix+=from[i];
        }
        from.splice(0,prefix.length);
        to.splice(0,prefix.length);
    
        i = from.length;
        while(i--) {
            tmp = prefix+from.slice(0,i).join('');
            if(from[i] == 'z') {
                tmp+='z';
            }
            else if(from.length-i == 1) {
                tmp += getRange(from[i],'z');
            }
            else if(i) {
                tmp += getRange(base36.charAt(base36.indexOf(from[i])+1),'z');
                tmp += '[0-9a-z]{'+(from.length-i-1)+'}';
            } 
            else {
                tmp += getRange(base36.charAt(base36.indexOf(from[i])+1),base36.charAt(base36.indexOf(to[i])-1));
                tmp += '[0-9a-z]{'+(from.length-i-1)+'}';
            }
            regex.push(tmp);
        }
        for(i=1,l=to.length;i<l;i++) {
            tmp = prefix+to.slice(0,i).join('');
            if(to[i] == '0') {
                tmp+='0';
            }
            else if(to.length-i == 1) {
                tmp += getRange('0',to[i]);
            }
            else {
                tmp += getRange('0',base36.charAt(base36.indexOf(to[i])-1));
                tmp += '[0-9a-z]{'+(from.length-i-1)+'}';
            } 
            regex.push(tmp);
        }
    
        return regex.join('|');
    }
    

    You can see it live here: http://jsfiddle.net/3cu52/3/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.