Skip to content
Generate a concise Regular Expression to match a list of words
TypeScript
Branch: develop
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
demo
src
.babelrc
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.npmrc
.prettierignore
.prettierrc.json
.publishrc
.travis.yml
README.md
package.json
tsconfig.json
tsconfig.types.json

README.md

strings-to-regex

npm package node version npm type definitions Build Status codecov Known Vulnerabilities

Generate a compact Regular Expression that matches a finite set.

Have you ever seen a dense Regular Expression like this one to match the 50 US state abbreviations?

/(A(L|K|Z|R)|C(A|O|T)|DE|FL|GA|HI|I(D|L|N|A)|K(S|Y)|LA|M(E|D|A|I|N|S|O|T)|N(E|V|H|J|M|Y|C|D)|O(H|K|R)|PA|RI|S(C|D)|T(N|X)|UT|V(T|A)|W(A|V|I|Y))/

This library generates patterns like that to match a list of strings you provide.

To reverse this process and list which strings a Regular Expression would match, try regex-to-strings.

Demo

API

condense(arrayOfStrings)

Generate a Regular Expression to match all strings in arrayOfStrings. Respects the casing of the strings. Returns a RegExp object.

import { condense } from 'strings-to-regex';

const stringsToMatch = ['foo', 'foobar', 'Foo', 'fooBarBaz'];
const matcher = condense(stringsToMatch);
console.log(matcher); // /(foo(|bar|BarBaz)|Foo)/

condenseIgnoreCase(arrayOfStrings)

A variation of condense() that ignores the casing of the strings. Returns a RegExp object.

import { condenseIgnoreCase } from 'strings-to-regex';

const stringsToMatch = ['foo', 'foobar', 'Foo', 'fooBarBaz'];
const matcher = condenseIgnoreCase(stringsToMatch);
console.log(matcher); // /foo(|bar(|baz))/i
You can’t perform that action at this time.