Generating strings from regular expression in JavaScript

If you're using JavaScript, there's Randexp which generates random strings that match a given regex.

Releases for browser


Use randexp.js, it does exactly what you want:

console.log(new RandExp(/^([a-zA-Z]){5}([0-9]){4}([a-zA-Z]){1}$/).gen());
console.log(new RandExp(/^[0-9]{4}$/).gen());
console.log(new RandExp(/^[0-9,A-Z]{4}$/).gen());
console.log(new RandExp(/^([A-Z]){5}([0-9]){4}([A-Z]){1}$/).gen());
<script src="https://github.com/fent/randexp.js/releases/download/v0.4.3/randexp.min.js"></script>