Need code to generate random value from any type of regex [ eg:[A-Za-z0-9]@[A-Za-z0-9].[A-Za-z]{2,3}

sachincalsoft
Tera Contributor

Hi team,

I need help to write a script in ServiceNow which will generate random value which will satisfy the give regex of any type.

Regex can be alpha numeric or or any format.

Example of the regex: [A-Za-z0-9]@[A-Za-z0-9].[A-Za-z]{2,3}

Please help

5 REPLIES 5

Nick Parsons
Mega Sage

Hi, please provide some code that you've tired

var RandomStringGenerator = Class.create();

 

RandomStringGenerator.prototype = {

    initialize: function() {

    },

 

    // Function to generate a random string based on a regex pattern

    generateRandomString: function(regexPattern) {

        var generatedString = "";

 

        // Loop through the regex pattern and generate a random character for each regex character

        for (var i = 0; i < regexPattern.length; i++) {

            var currentChar = regexPattern.charAt(i);

 

            // Handle different characters in the regex pattern

            switch (currentChar) {

                case 'A': // Uppercase letter

                    generatedString += this.generateRandomCharacter('ABCDEFGHIJKLMNOPQRSTUVWXYZ');

                    break;

                case 'a': // Lowercase letter

                    generatedString += this.generateRandomCharacter('abcdefghijklmnopqrstuvwxyz');

                    break;

                case '0': // Digit

                    generatedString += this.generateRandomCharacter('0123456789');

                    break;

                // Add more cases as needed for other regex characters

                default:

                    // If the character is not a regex character, append it to the generated string

                    generatedString += currentChar;

                    break;

            }

        }

 

        return generatedString;

    },

 

    // Function to generate a random character from a given character set

    generateRandomCharacter: function(characterSet) {

        var randomIndex = Math.floor(Math.random() * characterSet.length);

        return characterSet.charAt(randomIndex);

    }

};

 

// Example usage

var regexPattern = "^[a-zA-Z0-9 ]+$"; // Example regex pattern

var randomStringGenerator = new RandomStringGenerator();

var generatedString = randomStringGenerator.generateRandomString(regexPattern);

gs.info("Generated String: " + generatedString);

To be able to handle this for any regular expression is going to be a lot of work. You're going to have to build a custom parser that will look at the regex and build a graph from it (first a generalised nondeterministic finite automaton (GNFA) and from there a finite automaton (FA)) which you can then traverse the edges and nodes of randomly to get from the starting state to your terminal node. You might be able to find 3rd party libraries that do this parsing for you (here's an example). But keep in mind, if you go with a 3rd party library, then it should ideally be up-to-date with the latest ECMAScript standard to support all the possible regular expression patterns).  

 

I would instead suggest looking at your problem and seeing if you really need to generalise your random string generator to work with any regular expression or whether you can generate strings for predetermined regular expressions (or perhaps simpler regexs). 

Prince Arora
Tera Sage
Tera Sage

@sachincalsoft 

 

Could you please provide an example of the desired output format?