regexpbuilder
v14.0.0
Published
Create regular expressions using chained methods.
Downloads
5
Readme
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.
import { find } from 'regexpbuilder';
const regex = find('$')
.min(1).digits()
.then('.')
.digit()
.digit();
regex.test('$10.00'); // true
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
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
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
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
The opposite of boundary().
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
An alias for find(). It exists for readability.
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
pattern: either a string or a RegExpBuilder object
Used with or(pattern).
const regex = either('house').or('apartment');
console.log(regex.test('house')); // true
pattern: either a string or a RegExpBuilder object
Used with either(pattern).
const regex = either('house').or('apartment');
console.log(regex.test('house')); // true
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
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
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
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
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
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
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
The opposite of from(chars). Matches any character that is not in the list specified.
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
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
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
The opposite of ahead(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
The opposite of behind(pattern).
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
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
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
Matches a line break. The singular version of lineBreaks(). It does not need to be preceeded by any quantity specifier.
Matches a line break. This method needs to be preceeded by min, max, or just.
Matches whitespace. This can be used by itself, or it can be preceeded by min, max, or just.
The opposite of whitespace().
Matches a tab. It is the singular version of tabs().
Matches a tab. This method needs to be preceeded by min, max, or just.
Matches a digit. It is the singular version of digits().
Matches a digit. This method needs to be preceeded by min, max, or just.
Matches a letter. This is the singular version of letters().
Matches a letter. This method needs to be preceeded by min, max, or just.
Matches a lower case letter. This is the singular version of lowerCaseLetters().
Matches a lower case letter. This method needs to be preceeded by min, max, or just.
Matches an upper case letter. This is the singular version of upperCaseLetters().
Matches an upper case letter. This method needs to be preceeded by min, max, or just.
Adds the dot all flag to the regular expression.
Adds the ignore case flag to the regular expression.
Adds the multiline flag to the regular expression.
Adds the global flag to the regular expression.
Adds the sticky flag to the regular expression.
Adds the unicode flag to the regular expression.