All Projects → trusktr → Regexr

trusktr / Regexr

For composing regular expressions without the need for double-escaping inside strings.

Programming Languages

javascript
184084 projects - #8 most used programming language
es6
455 projects

Projects that are alternatives of or similar to Regexr

Regexp2
A full-featured regex engine in pure Go based on the .NET engine
Stars: ✭ 389 (+633.96%)
Mutual labels:  regex, regular-expression, regexp
Picomatch
Blazing fast and accurate glob matcher written JavaScript, with no dependencies and full support for standard and extended Bash glob features, including braces, extglobs, POSIX brackets, and regular expressions.
Stars: ✭ 393 (+641.51%)
Mutual labels:  regex, regular-expression, regexp
Regaxor
A regular expression fuzzer.
Stars: ✭ 35 (-33.96%)
Mutual labels:  regex, regexp, regular-expression
Regexpu
A source code transpiler that enables the use of ES2015 Unicode regular expressions in ES5.
Stars: ✭ 201 (+279.25%)
Mutual labels:  regex, regular-expression, regexp
RgxGen
Regex: generate matching and non matching strings based on regex pattern.
Stars: ✭ 45 (-15.09%)
Mutual labels:  regex, regexp, regular-expression
Regex For Regular Folk
🔍💪 Regular Expressions for Regular Folk — A visual, example-based introduction to RegEx [BETA]
Stars: ✭ 242 (+356.6%)
Mutual labels:  regex, regular-expression, regexp
regexp-expand
Show the ELisp regular expression at point in rx form.
Stars: ✭ 18 (-66.04%)
Mutual labels:  regex, regexp, regular-expression
Hyperscan Java
Match tens of thousands of regular expressions within milliseconds - Java bindings for Intel's hyperscan 5
Stars: ✭ 66 (+24.53%)
Mutual labels:  regex, regular-expression, regexp
Onigmo
Onigmo is a regular expressions library forked from Oniguruma.
Stars: ✭ 536 (+911.32%)
Mutual labels:  regex, regular-expression, regexp
globrex
Glob to regular expression with support for extended globs.
Stars: ✭ 52 (-1.89%)
Mutual labels:  regex, regexp, regular-expression
Grex
A command-line tool and library for generating regular expressions from user-provided test cases
Stars: ✭ 4,847 (+9045.28%)
Mutual labels:  regex, regular-expression, regexp
Regulex
🚧 Regular Expression Excited!
Stars: ✭ 4,877 (+9101.89%)
Mutual labels:  regex, regular-expression, regexp
Regex Dos
👮 👊 RegEx Denial of Service (ReDos) Scanner
Stars: ✭ 143 (+169.81%)
Mutual labels:  regex, regular-expression, regexp
moar
Deterministic Regular Expressions with Backreferences
Stars: ✭ 19 (-64.15%)
Mutual labels:  regex, regexp, 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 (+94.34%)
Mutual labels:  regex, regular-expression, regexp
cregex
A small implementation of regular expression matching engine in C
Stars: ✭ 72 (+35.85%)
Mutual labels:  regex, regexp, regular-expression
Emoji Regex
A regular expression to match all Emoji-only symbols as per the Unicode Standard.
Stars: ✭ 1,134 (+2039.62%)
Mutual labels:  regex, regular-expression, regexp
expand-brackets
Expand POSIX bracket expressions (character classes) in glob patterns.
Stars: ✭ 26 (-50.94%)
Mutual labels:  regex, regexp, regular-expression
Rex
Your RegEx companion.
Stars: ✭ 283 (+433.96%)
Mutual labels:  regex, regular-expression, regexp
Commonregex
🍫 A collection of common regular expressions for Go
Stars: ✭ 733 (+1283.02%)
Mutual labels:  regex, regular-expression, regexp

regexr

Easily compose regular expressions. Doing this with strings would otherwise be tedious due to having to double-escape things.

Basic example:

import r from "regexr";

const int = /\d+/;
const USD = r`\$${int}(\.${int})?`; // f.e. $3.45 or $5

(Note that int is an instance of RegExp and can be composed into the template string, and the resulting USD is also a RegExp)

Regexr provides an ES6 template tag function that makes it easy to compose RegExps using template strings without double-escaped hell.

In ES5 and below, we may try to compose the regular expressions like so:

const int = '\d+'
let USD = new RegExp('\$'+int+'(\.'+int+')?`) // this won't work!

but if you're experienced enough, you'd know that if you want to compose regular expressions using ES5 strings you have to escape the escape:

const int = '\\d+'
const USD = new RegExp('\\$'+int+'(\\.'+int+')?`) // correct!

Imagine making more complex regexes! For example, compare the following two examples achieving the same thing in ES5 and ES6 respectively:

// in ES5, the double escaping can get confusing:
var spaceRegex = "\\s*";
var finalRegex = "\\(" + spaceRegex + "\\/\\[\\\\\\d+\\]\\)*$";
finalRegex = new RegExp(finalRegex, "g");
console.log(!!"( /[\\12358])".match(finalRegex)); // true
// in ES6, we don't have to double escape, thanks to regexr:
import r from "regexr";

var spaceRegex = r`\s*`;
var finalRegex = r`/\(${spaceRegex}\/\[\\\d+\]\)*$/g`;
console.log(!!"( /[\\12358])".match(finalRegex)); // true

API

r`` template tag function

import r from "regexr";
// or
const r = require("regexr").default;

r`` is a template tag function that converts the given string into a RegExp without requiring double escaping. Instances of RegExp can be mixed into the string, and will be composed into the final RegExp.

Example:

const digit = /\d/;
const integer = r`/${digit}+/`;
const number = r`/${integer}|${digit}*\.${integer}|${integer}\.${digit}*/`; // f.e. 4.2, .5, 5.

Helpers

r.escape

Escape a plain string for matching literally inside a regex.

Sometimes we want to match an exact string that may contain symbols that we need to escape in order to match the characters of the string literally.

In the follow example, we want to find occurrences of the string "value: $5.00" in some input, so we need to escape the money string so that the dollar symbol ($) doesn't represent end-of-line and the period (.) doesn't mean any character:

const money = "$5.00";
const fiveDollarRegex = r`value: ${r.escape(money)}`;

console.log(fiveDollarRegex); // /value: \$5\.00/
console.log("value: $5.00".match(fiveDollarRegex)); // true
console.log("value: $5.50".match(fiveDollarRegex)); // false

Hand-picked Regexes

Regexr comes with some pre-selected regular expressions. For example, we can rewrite the first example:

import r from "regexr";

const USD = r`\$${r.integer}(\.${r.integer})?`; // f.e. $3.45 or $5

where r.integer is an instance of RegExp.

NOTE! Some of the following RegExps require to be wrapped in () when they are being composed into bigger RegExps. These will be noted below.

r.identifier

Matches a valid JavaScript identifier. See this for details.

Requires wrapping in () when being composed.

For example, to match a the beginning of a JS variable declaration, you could write:

const variableDeclaration = r`(const|let|var)\s+(${r.identifier})\s*=`;
!!"const foo  =".match(variableDeclaration); // true
!!"const foo bar =".match(variableDeclaration); // false

r.digit

Matches a single numerical digit (0-9).

Example:

!!" 8 ".match(r` ${r.digit} `); // true
!!" 25 ".match(r` ${r.digit} `); // false

r.integer

Matches 1 or more digits.

Example:

!!" 432 ".match(r` ${r.integer} `); // true

r.number

Matches a JavaScript Number.

Example:

!!"3".match(r.number); // true
!!"432".match(r.number); // true
!!"4.2".match(r.number); // true
!!"5.".match(r.number); // true
!!".34".match(r.number); // true

r.identifierList

Matches a comma separated list of legal JavaScript identifiers.

Example:

const identifiersInsideParens = r`\(${r.identifierList}\)`;

!!"(foo,  bar,baz)".match(identifiersInsideParens); // true
!!"(foo, ,bar, baz)".match(identifiersInsideParens); // false

r.functionHeader

Matches a JavaScript function header.

Example:

const identifiersInsideParens = r`\(${r.identifierList}\)`;

!!"function() {".match(r.functionHeader); // true
!!"function asdf() {".match(r.functionHeader); // true
!!"function (asdf ) {".match(r.functionHeader); // true
!!"function asdf (asdf ) {".match(r.functionHeader); // true
!!"function asdf(asdf  , asdf, ) {".match(r.functionHeader); // true
!!"function (asdf, asdf, asdfa asdf ) {".match(r.functionHeader); // false
!!"function asdf (asdf, asdf, asdfa asdf ) {".match(r.functionHeader); // false
!!"function asdf asdf (asdf, asdf, asdfa ) {".match(r.functionHeader); // false
!!"function asdf asdf (, asdf, asdf,) {".match(r.functionHeader); // false
!!"function (asdf asdf) {".match(r.functionHeader); // false
!!"function (asdf,,) {".match(r.functionHeader); // false
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].