A small library of functions to facilitate easier use of Regular Expressions in JS.
NB: This project takes advantage of the new test feature of Node.JS 20+.
A given string can be confirmed as being a valid Regular Expression using this predicate function.
E.g.
isRegExpPattern(); // = false
isRegExpPattern('['); // = false
isRegExpPattern('Hello, World!'); // = true
isRegExpPattern('^Hello,\\sWorld!?$'); // = true
isRegExpPattern('^[Hello,\\sWorld!?$'); // = true
The regExpString tagged template (function) enables the preparation of a reg exp pattern (string) without the complication of additional escape characaters.
E.g.
regExpString`^Hello,\sWorld!?$` = '^Hello,\\sWorld!?$'
This function facilitates the creation of a JS RegExp
object using a string pattern with formatting to aid comprehension and maintainability. This is achieved through the use of a template literal.
E.g.
When testString = 'Hello, WorLd!'
regExpTemplate()`(
[\sow]
)`;
yields /([\sl])/
, which when used to split testString gives the result of
['Hell', 'o', ',', ' ', 'W', 'o', 'rld!']; // length: 7
With the 'ignore case' flag configured:
regExpTemplate('i')`(
[\sow]
)`;
the regExp returned yields the following split:
['Hell', 'o', ',', ' ', '', 'W', '', 'o', 'rLd!']; // length: 9
In The following example, we are preparing the RegExp without flags but the pattern includes as full-length comment.
testRETemplate`(
# Full-line comment
[\sow]
)`;
The RegExp returned would be be the same as the first example as the (comment) line starting with a hash (#) character is removed/ignored.
Comments can start from midway of a line (to the end) but this means hash characters in the pattern have to be escaped.
When testString = 'Hello,#WorLd!'
testRETemplate`(
[\#ow] # Mid-line comment
)`;
returns:
['Hell', 'o', ',', '#', 'W', 'o', 'rLd!']; // length: 9