rett
Regular Expressions Template Tag
Features
- Removes need to use double backslash.
- Escapes special regex characters.
- Choosing when to escape special characters.
- Multiline regex with comments and indents.
- Utility for making complex regex.
Usage
Basic Use
The re
function can either take a template tag or be called normally.
When called normally, it takes regex flags and can only take a template tag from then on.
const re = ; console;// => /\d+\b/ console;// => /\b[a-z]\b/gi
Substitutions
Interpolate values just like any other template string.
All values will automatically be escaped.
const text = '?+{[()'; console;// => /\?\+\{\[\(\) \d+/
Multiline Regex
The re.line
function is used for multiline regex.
It has support for indents as well as single-line comments.
It works in the same way as the re
function.
console; // => /(\d+[A-Z])/
Ignoring Escape
Ignore escapes if you are planning on building regex bit by bit.
This is done with the re.ignore
function which can take either a string or a template tag.
The re.raw
function is also available for disabling escaping altogether.
const special = '{0,2}'; console;// => /1{0,2}/ console;// => /1{0,2}/ const digits = reignore`\d+`; console;// => /-?\d+/
Utility
re.escape()
Escapes special regex characters from a string.
Can take a template tag and will escape interpolated values.
console;// => 'What\\?'
re.unescape()
Unescapes special regex characters from a string.
Can take a template tag and will unescape interpolated values.
Note that this is not a perfect reversal.
console;// => 'What?'
re.join()
Joins an array together by a character.
Escapes the values of the array.
Useful for making different possibilties with |
in regex.
const list = re;const regex = re``; console;// => /\(|\)|\?/
Options
re.options
You can set the default options here.
escape
: default for escaping substitutions.multiline
: default for parsing as multiline.debug
: logs to console whenever a regex is made.flags.default
: default for flags if no flags provided.flags.addtion
: flags to add onto exisiting flags.
reoptions = escape: true multiline: true debug: true flags: default: 'u' addition: 'u' ; console; // => /ABC(\d)/u
re.specialEscapeRegex
Regex for escaping special characters.
Reassign with your own if needed.
re.RegExp
Constructor used for creating new regular expression instances.
Reassign with your own if needed.
Other
re.create()
Creates a new regex from the values inputted.
Used internally.
console;// => /a1b/g
re.isTemplateTag()
Checks if a value is a template tag.
Used internally.
console;// => true