Important
This repository is moved to Codeberg.org.
For now it is kept in sync with the original repository @Github.
Depending on future USA/Microsoft/Github policies the Github version maybe discontinued later.
In other words, starting july 12 2025 the Codeberg repository is authorative.
A module to create JS regular expressions with comments and whitespace using a tagged template
import $RE from "[location of RegexpCreator.js]";const noCommentRE = $RE`
// -------------------------------------------------------------------
// this regular expression finds all comment sections in a JS-string
// -------------------------------------------------------------------
\/\* ( ?: [^*] | \*+[^*\/] ) *\*+\/ // multiline (/* ... */)
| // or
( ?<!:|\\\|' ) \/\/.* // single line (// ...)`;
// noCommentRE => /\/\*(?:[^*]|\*+[^*\/])*\*+\/|(?<!:|\\\|')\/\/.*/gm
const cleaned = "Hello world /* no comment */\nHello // no world"
.replace(noCommentRE.flags(`g`), ``)
// cleaned => Hello world\nHelloThe imported function (here $RE) has a static escape method to escape strings for
a regular expression.
It either uses the new (generally available) RegExp.escape or a proprietary escape method for older browsers.
$RE.escape(`Hello (world)`); // result =>
// using RegExp.escape: "\x48ello\x20\(world\\)"
// using proprietary method: "\Hello\x20\(world\)";A $RE-instance is actually a Proxy.
It can be used as a regular expression, but also contains a flags method and a getter (re)
for the actual regular expression value.
The [$RE instance].re getter return the actual RegExp instance.
Flags can be added using the instance flags method
[$RE RegEx instance].flags([flags:string])
or (legacy) by adding an Array of flag strings as last replacement in the template string
e.g. $RE`[ABZ].+ ${["g", "u"]}` .
To remove all current flags, use [$RE RegEx instance].flags("-r").
The flags method is chainable. So [$RE RegEx instance].flags("-r").flags("gm")
removes current flags from the instance and re-adds gm.
Replacing flags in the instance can be accomplished using the above mentioned (chaining)
or [$RE RegEx instance].flags("-r|[new flags]") (so remove current flags -r, then
add |[new flags], e.g. [$RE RegEx instance].flags(-r|iu).
Notes
- The replacement value must be the last replacement in the template string.
- The given flags are checked and cleaned
To insert a plain space (so not a whitespace) use <!32>
const hiRE = $RE`
Hello<!32> /* <= this will be a plain space in the result */ {1,}
.*`.flags(`gi`);
// hiRE => /Hello {1,}.*/gi
const hi = hiRE.test("hello world!");
// hi => trueOr use $re.escape
const hiRE = $RE`
Hello${$re.escape(` `)} /* <= this will be \x20 in the result */ {1,}
.*`.flags(`gi`);
// hiRE => /Hello\x20{1,}.*/gi
hiRE.test("hello world!"); // => true