Generate random strings using Regular Expressions in Nodejs

There are times when you need to generate random strings in your application. Maybe it is to fill a database table or an array for testing various algorithms. Regular Expressions are a powerful language tool for text searching and can also be used to generate strings based on a expression.

randexp.js is a JavaScript library which can be used to generate random strings and also numbers. You can use it on the browser or Nodejs.


For using with Nodejs use npm.

npm install randexp

For working in the browser use the minified version.

Generating random numbers

The following short code will show you how to generate a random 6 digit number. Initially we define the regular expression that matches a number with 6 digits. Then we call the gen() method of the ‘RandExp’ object to generate the random string.

RandExp = require('randexp');
randexp = new RandExp(/\d\d\d\d\d\d/);

If we want a 3 digit random number we can change the regular expression accordingly.

randexp = new RandExp(/\d\d\d/);

To generate list of numbers we can use the following.

randexp = new RandExp(/\d\d\d/);
for(i=0; i<10;i++) {

You may have noticed that some numbers start with a zero, which you may not want. That can be easily corrected by changing the regular expression.

randexp = new RandExp(/[1-9]\d\d/);

This will make sure that the leading digit is always between 1-9. Say you want to generate for testing purposes a random US domestic telephone number in the format like ‘(541) 754-3010’. For this we can use the following expression.

randexp = new RandExp(/\([1-9]\d\d\) [1-9]\d\d-[1-9]\d\d\d/);
(413) 330-9071

In this way you can generate various styles of random numbers.

Generating random strings

To generate a random string use the following expression. This will generate a random string of 6 characters consisting of lowercase alphabets.

const randexp = new RandExp(/[a-z]{6}/);

The {6} is a repetition operator that allows you to specify how many characters to repeat the last given expression. So, to generate a random string between 3-9 characters you can specify the following.

const randexp = new RandExp(/[a-z]{3,9}/);
/* sample output:

Below are some sample expressions for emitting random strings. Each expression is gnerated 5 times so that we can see how random a string each expression provides.

// grouping and piping
const randexp = new RandExp(/hello+ (world|to you)/);
/* output
hellooooooooooooo to you
helloooooooooooooooooooooooooooooooooooooooo world
helloooooooooo to you
hellooooooooooooooooooooooo to you
helloooooooooooooooooooooooooooooooo to you
// Random stuff using wildcards
const randexp = new RandExp(/.+/);
/* output
g#. pC[m*#<;_7V@_gMac*9&_{LL`,&Wg+: iYBFi4]xB>4zv@G6|]jz{0
ys8/&^*RiI\L<EN2|>WuUC&hZ4J!>;)^x6fZ6 MFJ+=2qpG&&IJ
// ignore case
const randexp = new RandExp(/Hello World!/i);
/* output
helLO WORLd!
HellO WorLD!
hElLo wOrLd!
hElLo WorlD!
hEllO worlD!
// sets and ranges
const randexp = new RandExp(/<([a-z]\w{0,20})> FOO-BAR <\1>/);
/* output
<qjqEGVs> FOO-BAR <qjqEGVs>
<ucZCSs2hoL_13Qc> FOO-BAR <ucZCSs2hoL_13Qc>
<c> FOO-BAR <c>
<nAxbNA7C9SYvmd7f3vZts> FOO-BAR <nAxbNA7C9SYvmd7f3vZts>
<q9> FOO-BAR <q9>
// dynamic regexp
const randexp = new RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day');
/* output


Generating passwords:

One application of random strings is to generate passwords for users. When users request a ‘forgot password’ query we can generate a one-time random password for them. A sample expression could look like below. The ‘\w’ metacharacter is used to specify a word character which is a character from a-z, A-Z, 0-9, including the _ (underscore) character.

// Generate a 9 character random 'word'
const randexp = new RandExp(/\w{9}/);
/* output:

Generating dates:
You can generate random dates using the following.

const RandExp = require('randexp');
const randexp = new RandExp(/(January|February|March|April|May|June|July|August|September|October|November|December) ([1-9]|[12][0-9]|3[01]), (19|20)[0-9][0-9]/);
for(i=0; i<5;i++) {
/* output:
January 5, 2011
December 26, 1985
February 30, 1935
September 18, 2033
October 20, 2046

The Pseudo Random Number Generator (PRNG)

The default randomness in the library is provided by the JavaScript Math.random() function. If you need to use another PRNG, you can override RandExp.prototype.randInt or randexp.randInt (where randexp is an instance of RandExp). randInt(from, to) accepts an inclusive range and returns a randomly selected number within that range.

Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method. For the password application mentioned above, wherein the one-time password expires in a short time this should not be a problem.

Syntactic sugar

If you’re only going to use gen() once with a regexp and want a slightly shorter syntax for it than you can use the syntactic sugar available.

const randexp = require('randexp').randexp;
random_string = randexp(/\w{9}/);
// OR
random_string = /\w{9}/.gen();

Infinite max range

The following code uses the ‘+’ metacharacter to repeat the ‘o’ character multiple times, with a limit of 100. This will repeat the ‘o’ in the expression below to a maximum of 100 times.

const randexp = new RandExp(/hello+ (world|to you)/);
for(i=0; i<5;i++) {
/* output
hellooooooooo to you
helloooo to you
helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo world
helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo world
helloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo world

You can change the max value of the repetition metacharacter by setting it as below. This also applies to the ‘*’ operator.

const randexp = new RandExp(/hello+ (world|to you)/);
// set max repetitions to 20
randexp.max = 20;
for(i=0; i<5;i++) {
/* output
hellooooooooooo to you
helloooo to you
helloooooooooo to you
hellooo to you
hellooooooooo world


As you have seen we can use the library to generate a diverse range of random strings of characters, numbers or a combination of other characters to create dummy data for testing or other security purposes.

One thought on “Generate random strings using Regular Expressions in Nodejs

Comments are closed.