All Projects → fent → Randexp.js

fent / Randexp.js

Licence: mit
Create random strings that match a given regular expression.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Randexp.js

Tempreites
One-file semantic DSL-free templates direto da roça for the browser and server.
Stars: ✭ 31 (-98.16%)
Mutual labels:  regular-expression
Eval Sql.net
SQL Eval Function | Dynamically Evaluate Expression in SQL Server using C# Syntax
Stars: ✭ 84 (-95.01%)
Mutual labels:  regular-expression
Oniguruma
regular expression library
Stars: ✭ 1,643 (-2.32%)
Mutual labels:  regular-expression
Regexr
For composing regular expressions without the need for double-escaping inside strings.
Stars: ✭ 53 (-96.85%)
Mutual labels:  regular-expression
Regen
Tool to generate random strings from Go/RE2 regular expressions (Migrated to https://git.sr.ht/~nilium/regen)
Stars: ✭ 79 (-95.3%)
Mutual labels:  regular-expression
Computer Science Resources
A list of resources in different fields of Computer Science (multiple languages)
Stars: ✭ 1,316 (-21.76%)
Mutual labels:  regular-expression
Flutter easy rich text
The EasyRichText widget provides an easy way to use RichText.
Stars: ✭ 30 (-98.22%)
Mutual labels:  regular-expression
Js Regular Expression Awesome
📄我收藏的正则表达式大全,欢迎补充
Stars: ✭ 120 (-92.87%)
Mutual labels:  regular-expression
Regex
A Regular Expression game for Android
Stars: ✭ 80 (-95.24%)
Mutual labels:  regular-expression
Automa.jl
A julia code generator for regular expressions
Stars: ✭ 111 (-93.4%)
Mutual labels:  regular-expression
Emoji Regex
A regular expression to match all Emoji-only symbols as per the Unicode Standard.
Stars: ✭ 1,134 (-32.58%)
Mutual labels:  regular-expression
Nanomatch
Fast, minimal glob matcher for node.js. Similar to micromatch, minimatch and multimatch, but without support for extended globs (extglobs), posix brackets or braces, and with complete Bash 4.3 wildcard support: ("*", "**", and "?").
Stars: ✭ 79 (-95.3%)
Mutual labels:  regular-expression
To Regex Range
Pass two numbers, get a regex-compatible source string for matching ranges. Fast compiler, optimized regex, and validated against more than 2.78 million test assertions. Useful for creating regular expressions to validate numbers, ranges, years, etc.
Stars: ✭ 97 (-94.23%)
Mutual labels:  regular-expression
Rexrex
🦖 Composable JavaScript regular expressions
Stars: ✭ 34 (-97.98%)
Mutual labels:  regular-expression
Regular
🔍The convenient paste of regular expression🔎
Stars: ✭ 118 (-92.98%)
Mutual labels:  regular-expression
Place2live
Analysis of the characteristics of different countries
Stars: ✭ 30 (-98.22%)
Mutual labels:  regular-expression
Globbing
Introduction to "globbing" or glob matching, a programming concept that allows "filepath expansion" and matching using wildcards.
Stars: ✭ 86 (-94.89%)
Mutual labels:  regular-expression
Dan Jurafsky Chris Manning Nlp
My solution to the Natural Language Processing course made by Dan Jurafsky, Chris Manning in Winter 2012.
Stars: ✭ 124 (-92.63%)
Mutual labels:  regular-expression
Tokenizer
Source code tokenizer
Stars: ✭ 119 (-92.93%)
Mutual labels:  regular-expression
Orchestra
One language to be RegExp's Successor. Visually readable and rich, technically safe and extended, naturally scalable, advanced, and optimized
Stars: ✭ 103 (-93.88%)
Mutual labels:  regular-expression

randexp.js

randexp will generate a random string that matches a given RegExp Javascript object.

Dependency Status codecov

Usage

const RandExp = require('randexp');

// supports grouping and piping
new RandExp(/hello+ (world|to you)/).gen();
// => hellooooooooooooooooooo world

// sets and ranges and references
new RandExp(/<([a-z]\w{0,20})>foo<\1>/).gen();
// => <m5xhdg>foo<m5xhdg>

// wildcard
new RandExp(/random stuff: .+/).gen();
// => random stuff: l3m;Hf9XYbI [YPaxV>U*4-_F!WXQh9>;rH3i l!8.zoh?[utt1OWFQrE ^~8zEQm]~tK

// ignore case
new RandExp(/xxx xtreme dragon warrior xxx/i).gen();
// => xxx xtReME dRAGON warRiOR xXX

// dynamic regexp shortcut
new RandExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i');
// is the same as
new RandExp(new RegExp('(sun|mon|tue|wednes|thurs|fri|satur)day', 'i'));

If you're only going to use gen() once with a regexp and want slightly shorter syntax for it

const randexp = require('randexp').randexp;

randexp(/[1-6]/); // 4
randexp('great|good( job)?|excellent'); // great

If you miss the old syntax

require('randexp').sugar();

/yes|no|maybe|i don't know/.gen(); // maybe

Motivation

Regular expressions are used in every language, every programmer is familiar with them. Regex can be used to easily express complex strings. What better way to generate a random string than with a language you can use to express the string you want?

Thanks to String-Random for giving me the idea to make this in the first place and randexp for the sweet .gen() syntax.

Default Range

The default generated character range includes printable ASCII characters.

ascii table

In order to add or remove characters, a defaultRange attribute is exposed. you can subtract(from, to) and add(from, to)

const randexp = new RandExp(/random stuff: .+/);
randexp.defaultRange.subtract(32, 126);
randexp.defaultRange.add(0, 65535);
randexp.gen();
// => random stuff: 湐箻ໜ䫴␩⶛㳸長���邓蕲뤀쑡篷皇硬剈궦佔칗븛뀃匫鴔事좍ﯣ⭼ꝏ䭍詳蒂䥂뽭

You can also change the default range by changing RandExp.prototype.defaultRange.

Custom PRNG

The default randomness is provided by Math.random(). If you need to use a seedable or cryptographic 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.

Infinite Repetitionals

Repetitional tokens such as *, +, and {3,} have an infinite max range. In this case, randexp looks at its min and adds 100 to it to get a useable max value. If you want to use another int other than 100 you can change the max property in RandExp.prototype or the RandExp instance.

const randexp = new RandExp(/no{1,}/);
randexp.max = 1000000;

With RandExp.sugar()

const regexp = /(hi)*/;
regexp.max = 1000000;

Bad Regular Expressions

There are some regular expressions which can never match any string.

  • Ones with badly placed positionals such as /a^/ and /$c/m. Randexp will ignore positional tokens.

  • Back references to non-existing groups like /(a)\1\2/. Randexp will ignore those references, returning an empty string for them. If the group exists only after the reference is used such as in /\1 (hey)/, it will too be ignored.

  • Custom negated character sets with two sets inside that cancel each other out. Example: /[^\w\W]/. If you give this to randexp, it will return an empty string for this set since it can't match anything.

Projects based on randexp.js

JSON-Schema Faker

Use generators to populate JSON Schema samples. See: jsf on github and jsf demo page.

Install

Node.js

npm install randexp

Browser

Download the minified version from the latest release.

Tests

Tests are written with mocha

npm test

Integration with TypeScript

RandExp includes TypeScript definitions.

import RandExp from "randexp";
const randexp = new RandExp(/[a-z]{6}/);
randexp.gen();

Use dtslint to check the definition file.

npm install -g dtslint
npm run dtslint
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].