regexpbuilder

14.0.0 • Public • Published

RegExpBuilder

RegExpBuilder integrates regular expressions into the programming language, thereby making them easy to read and maintain. Regular Expressions are created by using chained methods and variables such as arrays or strings.

Installation

Here are a couple of examples using Javascript:

Money

import { find } from 'regexpbuilder';

const regex = find('$')
  .min(1).digits()
  .then('.')
  .digit()
  .digit();
  
regex.test('$10.00'); // true

Nested patterns

import { min, just } from 'regexpbuilder';

const pattern = min(1).of('p')
  .min(2).of('q');

const regex = just(2).like(pattern);

regex.test('pqqpqq'); // true

API documentation

start()

Matches the start of a line or the start of input depending on whether multiline is set.

const regex = start().then('the');

regex.test('the bear has claws.'); // true
regex.test('bears know where the honey is.'); // false

end()

Matches the end of a line or the end of input depending on whether multiline is set.

const regex = find('house').end();

regex.test('The house'); // true
regex.test('The house is on the hill'); // false

boundary()

Matches a word boundary. This is the place before or after a word.

const regex = find('cure').boundary();

regex.test('cure.'); // true
regex.test('The cure is a band.'); // true
regex.test('Thecureisaband'); // false

nonBoundary()

The opposite of boundary().

find(pattern)

pattern: either a string or a RegExpBuilder object

Matches the string or pattern once.

const money = find('$').min(1).digits();

const regex = find(money)
  .then(' ')
  .then('per hour');

regex.test('$100 per hour'); // true

then(pattern)

An alias for find(). It exists for readability.

maybe(pattern)

pattern: either a string or a RegExpBuilder object

Matches the string or pattern at most once.

const regex = find('http')
  .maybe('s')
  .then(' is a protocol');

regex.test('http is a protocol'); // true
regex.test('https is a protocol'); // true

either(pattern)

pattern: either a string or a RegExpBuilder object

Used with or(pattern).

const regex = either('house').or('apartment');

console.log(regex.test('house')); // true

or(pattern)

pattern: either a string or a RegExpBuilder object

Used with either(pattern).

const regex = either('house').or('apartment');

console.log(regex.test('house')); // true

just(count)

count: an integer

Matches the pattern exactly count number of times.

const regex = just(3).of('p')
  .just(2).from('pqr');

regex.test('ppprr'); // true
regex.test('pppprr'); // false

min(count)

count: an integer

Matches the pattern at least count number of times. Can be combined with max(count) to specify an exact range.

const regex = min(3).digits();

regex.test('1000'); // true
regex.test('9'); // false

max(count)

count: an integer

Matches the pattern at most count number of times. Can be combined with min(count) to specify an exact range.

const regex = min(3).max(5).letters();

regex.test('is'); // false
regex.test('household'); // false
regex.test('tree'); // true

of(text)

text: a string

Matches the supplied string. This method needs to be preceeded by min, max, or just.

const regex = just(2).of('20');

regex.test('2020'); // true
regex.test('2021'); // false

ofAny()

Matches any character. This method needs to be preceeded by min, max, or just.

const regex = find('<')
  .just(4).ofAny()
  .then('>');

regex.test('<body>'); // true
regex.test('<list>'); // true
regex.test('<li>'); // false

ofGroup(label)

label: a string used to refer to the group when needed

A reference to a group that was previously captured using asGroup(label).

const regex = just(1).letter().asGroup('letter')
  .then(' ')
  .just(1).ofGroup('letter');

regex.test('This seems'); // true
regex.test('Where is'); // false

from(chars)

chars: a string or an array. Ranges can also be used, such as 'p-rv', or ['p-r', 'v'], both of which will match p, q, r, and v.

Matches any character that is specified in the list of characters. This method must be preceeded by min, max, or just.

const regex = just(3).from('pqr');

regex.test('ppp'); // true
regex.test('prr'); // true
regex.test('prs'); // false

notFrom(chars)

The opposite of from(chars). Matches any character that is not in the list specified.

like(builder)

builder: a RegExpBuilder object

This method is used for nesting patterns.

const pattern = min(1).of('p')
  .min(2).of('q');

const regex = just(2).like(pattern);

regex.test('ppqqqpppqqqq'); // true

reluctantly()

Specifies that the pattern should be matched reluctantly, as opposed to greedily.

const regex = min(3).letters().reluctantly()
  .then(' hill');

console.log(regex.exec('The hill was a large hill')[0]); // The hill

ahead(pattern)

pattern: a string or RegExpBuilder object

A lookahead. It scans ahead for the pattern, but does not include that pattern in the matched result.

const regex = find('bus').ahead('load');

console.log(regex.exec('busload')[0]); // bus

notAhead(pattern)

The opposite of ahead(pattern).

behind(pattern)

pattern: a string or RegExpBuilder object

A lookbehind. It scans behind the current match to see if the pattern exists, but does not include that pattern in the matched results.

const regex = behind('<')
  .find('body')
  .ahead('>');

console.log(regex.exec('<body>')[0]); // body

notBehind

The opposite of behind(pattern).

asGroup(label)

label: a string

Captures the pattern and gives it a label.

const pattern = either('house').or('apartment');

const regex = find('The ')
  .then(pattern).asGroup('dwelling');

console.log(regex.exec('The house').groups.dwelling); // house

withProp(prop)

prop: a string that represents a unicode property

Matches a unicode character with the specified property. The unicode flag must be added to the regular expression for this to work.

const regex = min(2).withProp('Emoji')
  .unicode();

regex.test('There are no emojis here.'); // false
regex.test('This has emojis 😂😍'); // true

any()

Matches any character once. The singular version of ofAny(). It does not need to be preceeded by any quantity specifier.

const regex = any();

regex.test('q'); // true
regex.test('r'); // true

lineBreak()

Matches a line break. The singular version of lineBreaks(). It does not need to be preceeded by any quantity specifier.

lineBreaks()

Matches a line break. This method needs to be preceeded by min, max, or just.

whitespace()

Matches whitespace. This can be used by itself, or it can be preceeded by min, max, or just.

nonWhitespace()

The opposite of whitespace().

tab()

Matches a tab. It is the singular version of tabs().

tabs()

Matches a tab. This method needs to be preceeded by min, max, or just.

digit()

Matches a digit. It is the singular version of digits().

digits()

Matches a digit. This method needs to be preceeded by min, max, or just.

letter()

Matches a letter. This is the singular version of letters().

letters()

Matches a letter. This method needs to be preceeded by min, max, or just.

lowerCaseLetter()

Matches a lower case letter. This is the singular version of lowerCaseLetters().

lowerCaseLetters()

Matches a lower case letter. This method needs to be preceeded by min, max, or just.

upperCaseLetter()

Matches an upper case letter. This is the singular version of upperCaseLetters().

upperCaseLetters()

Matches an upper case letter. This method needs to be preceeded by min, max, or just.

dotAll()

Adds the dot all flag to the regular expression.

ignoreCase()

Adds the ignore case flag to the regular expression.

multiLine()

Adds the multiline flag to the regular expression.

global()

Adds the global flag to the regular expression.

sticky()

Adds the sticky flag to the regular expression.

unicode()

Adds the unicode flag to the regular expression.

Readme

Keywords

Package Sidebar

Install

npm i regexpbuilder

Weekly Downloads

0

Version

14.0.0

License

MIT

Unpacked Size

27.6 kB

Total Files

5

Last publish

Collaborators

  • thebinarysearchtree