Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

regex

Package Overview
Dependencies
Maintainers
0
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

regex - npm Package Compare versions

Comparing version 4.2.0 to 4.2.1

4

dist/regex.min.js

@@ -1,2 +0,2 @@

var Regex=(()=>{var H=Object.defineProperty;var Ce=Object.getOwnPropertyDescriptor;var Ie=Object.getOwnPropertyNames;var Le=Object.prototype.hasOwnProperty;var _e=(e,t)=>{for(var n in t)H(e,n,{get:t[n],enumerable:!0})},be=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ie(t))!Le.call(e,o)&&o!==n&&H(e,o,{get:()=>t[o],enumerable:!(r=Ce(t,o))||r.enumerable});return e};var Oe=e=>be(H({},"__esModule",{value:!0}),e);var ze={};_e(ze,{pattern:()=>V,processRegex:()=>qe,regex:()=>je});var E=Object.freeze({DEFAULT:"DEFAULT",CHAR_CLASS:"CHAR_CLASS"});function N(e,t,n,r){let o=new RegExp(String.raw`${t}|(?<$skip>\[\^?|\\?.)`,"gsu"),i=[!1],s=0,a="";for(let u of e.matchAll(o)){let{0:c,groups:{$skip:p}}=u;if(!p&&(!r||r===E.DEFAULT==!s)){n instanceof Function?a+=n(u,{context:s?E.CHAR_CLASS:E.DEFAULT,negated:i[i.length-1]}):a+=n;continue}c[0]==="["?(s++,i.push(c[1]==="^")):c==="]"&&s&&(s--,i.pop()),a+=c}return a}function k(e,t,n,r){N(e,t,n,r)}function y(e,t,n=0,r){if(!new RegExp(t,"su").test(e))return null;let o=new RegExp(`${t}|(?<$skip>\\\\?.)`,"gsu");o.lastIndex=n;let i=0,s;for(;s=o.exec(e);){let{0:a,groups:{$skip:u}}=s;if(!u&&(!r||r===E.DEFAULT==!i))return s;a==="["?i++:a==="]"&&i&&i--,o.lastIndex==s.index&&o.lastIndex++}return null}function v(e,t,n){return!!y(e,t,0,n)}function W(e,t){let n=/\\?./gsu;n.lastIndex=t;let r=e.length,o=0,i=1,s;for(;s=n.exec(e);){let[a]=s;if(a==="[")o++;else if(o)a==="]"&&o--;else if(a==="(")i++;else if(a===")"&&(i--,!i)){r=s.index;break}}return e.slice(t,r)}var $=class{#e;constructor(t){this.#e=t}toString(){return String(this.#e)}};function V(e,...t){if(Array.isArray(e?.raw))return new $(e.raw.flatMap((n,r)=>r<e.raw.length-1?[n,t[r]]:n).join(""));if(!t.length)return new $(e??"");throw new Error(`Unexpected arguments: ${JSON.stringify([e,...t])}`)}var f={DEFAULT:"R_DEFAULT",CHAR_CLASS:"R_CHAR_CLASS",GROUP_NAME:"R_GROUP_NAME",ENCLOSED_TOKEN:"R_ENCLOSED_TOKEN",INTERVAL_QUANTIFIER:"R_INTERVAL_QUANTIFIER",INVALID_INCOMPLETE_TOKEN:"R_INVALID_INCOMPLETE_TOKEN"},d={DEFAULT:"CC_DEFAULT",RANGE:"CC_RANGE",ENCLOSED_TOKEN:"CC_ENCLOSED_TOKEN",Q_TOKEN:"CC_Q_TOKEN",INVALID_INCOMPLETE_TOKEN:"CC_INVALID_INCOMPLETE_TOKEN"},x=(()=>{try{new RegExp("(?i:)")}catch{return!1}return!0})(),oe=(()=>{try{new RegExp("","v")}catch{return!1}return!0})(),b="&!#$%*+,.:;<=>?@^`~",C="$E$",Q=String.raw`\(\?<(?![=!])(?<captureName>[^>]+)>`,O=String.raw`\((?!\?)(?!(?<=\(\?\()DEFINE\))|${Q}`,R=String.raw`\(\?(?:[:=!>A-Za-z\-]|<[=!]|\(DEFINE\))`;function B(e,t){return t===E.CHAR_CLASS?e.replace(new RegExp(String.raw`[()\[\]{}|\\/\-${b}]`,"g"),"\\$&"):e.replace(/[()\[\]{}|\\^$*+?.]/g,"\\$&")}function j(e){return e.replace(new RegExp(`^([${b}])(?!\\1)`),(t,n,r)=>`\\${t}${r+1===e.length?"":t}`)}function se(e){return e.replace(/^\^/,"\\^^")}function G(e,t){return N(e,String.raw`\\0(?!\d)`,"\\u{0}",t)}function ne(e,t,n){let r=0;for(let[o]of e.matchAll(new RegExp(`[${B(t+n,E.CHAR_CLASS)}]`,"g")))if(r+=o===t?1:-1,r<0)return n;return r>0?t:""}function ie(e,t,n){let r=e.replace(/\\./gsu,"");if(r.endsWith("\\"))return"\\";if(t===f.DEFAULT)return ne(r,"(",")");if(t===f.CHAR_CLASS&&!(n===d.ENCLOSED_TOKEN||n===d.Q_TOKEN))return ne(r,"[","]");if(t===f.ENCLOSED_TOKEN||t===f.INTERVAL_QUANTIFIER||n===d.ENCLOSED_TOKEN||n===d.Q_TOKEN){if(r.includes("}"))return"}"}else if(t===f.GROUP_NAME&&r.includes(">"))return">";return""}var re=new RegExp(String.raw`
var Regex=(()=>{var H=Object.defineProperty;var Ce=Object.getOwnPropertyDescriptor;var Ie=Object.getOwnPropertyNames;var Le=Object.prototype.hasOwnProperty;var _e=(e,t)=>{for(var n in t)H(e,n,{get:t[n],enumerable:!0})},be=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ie(t))!Le.call(e,o)&&o!==n&&H(e,o,{get:()=>t[o],enumerable:!(r=Ce(t,o))||r.enumerable});return e};var Oe=e=>be(H({},"__esModule",{value:!0}),e);var ze={};_e(ze,{pattern:()=>V,processRegex:()=>qe,regex:()=>je,rewrite:()=>qe});var E=Object.freeze({DEFAULT:"DEFAULT",CHAR_CLASS:"CHAR_CLASS"});function N(e,t,n,r){let o=new RegExp(String.raw`${t}|(?<$skip>\[\^?|\\?.)`,"gsu"),i=[!1],s=0,a="";for(let u of e.matchAll(o)){let{0:c,groups:{$skip:p}}=u;if(!p&&(!r||r===E.DEFAULT==!s)){n instanceof Function?a+=n(u,{context:s?E.CHAR_CLASS:E.DEFAULT,negated:i[i.length-1]}):a+=n;continue}c[0]==="["?(s++,i.push(c[1]==="^")):c==="]"&&s&&(s--,i.pop()),a+=c}return a}function k(e,t,n,r){N(e,t,n,r)}function y(e,t,n=0,r){if(!new RegExp(t,"su").test(e))return null;let o=new RegExp(`${t}|(?<$skip>\\\\?.)`,"gsu");o.lastIndex=n;let i=0,s;for(;s=o.exec(e);){let{0:a,groups:{$skip:u}}=s;if(!u&&(!r||r===E.DEFAULT==!i))return s;a==="["?i++:a==="]"&&i&&i--,o.lastIndex==s.index&&o.lastIndex++}return null}function v(e,t,n){return!!y(e,t,0,n)}function W(e,t){let n=/\\?./gsu;n.lastIndex=t;let r=e.length,o=0,i=1,s;for(;s=n.exec(e);){let[a]=s;if(a==="[")o++;else if(o)a==="]"&&o--;else if(a==="(")i++;else if(a===")"&&(i--,!i)){r=s.index;break}}return e.slice(t,r)}var $=class{#e;constructor(t){this.#e=t}toString(){return String(this.#e)}};function V(e,...t){if(Array.isArray(e?.raw))return new $(e.raw.flatMap((n,r)=>r<e.raw.length-1?[n,t[r]]:n).join(""));if(!t.length)return new $(e??"");throw new Error(`Unexpected arguments: ${JSON.stringify([e,...t])}`)}var f={DEFAULT:"R_DEFAULT",CHAR_CLASS:"R_CHAR_CLASS",GROUP_NAME:"R_GROUP_NAME",ENCLOSED_TOKEN:"R_ENCLOSED_TOKEN",INTERVAL_QUANTIFIER:"R_INTERVAL_QUANTIFIER",INVALID_INCOMPLETE_TOKEN:"R_INVALID_INCOMPLETE_TOKEN"},d={DEFAULT:"CC_DEFAULT",RANGE:"CC_RANGE",ENCLOSED_TOKEN:"CC_ENCLOSED_TOKEN",Q_TOKEN:"CC_Q_TOKEN",INVALID_INCOMPLETE_TOKEN:"CC_INVALID_INCOMPLETE_TOKEN"},x=(()=>{try{new RegExp("(?i:)")}catch{return!1}return!0})(),oe=(()=>{try{new RegExp("","v")}catch{return!1}return!0})(),b="&!#$%*+,.:;<=>?@^`~",C="$E$",Q=String.raw`\(\?<(?![=!])(?<captureName>[^>]+)>`,O=String.raw`\((?!\?)(?!(?<=\(\?\()DEFINE\))|${Q}`,R=String.raw`\(\?(?:[:=!>A-Za-z\-]|<[=!]|\(DEFINE\))`;function B(e,t){return t===E.CHAR_CLASS?e.replace(new RegExp(String.raw`[()\[\]{}|\\/\-${b}]`,"g"),"\\$&"):e.replace(/[()\[\]{}|\\^$*+?.]/g,"\\$&")}function j(e){return e.replace(new RegExp(`^([${b}])(?!\\1)`),(t,n,r)=>`\\${t}${r+1===e.length?"":t}`)}function se(e){return e.replace(/^\^/,"\\^^")}function G(e,t){return N(e,String.raw`\\0(?!\d)`,"\\u{0}",t)}function ne(e,t,n){let r=0;for(let[o]of e.matchAll(new RegExp(`[${B(t+n,E.CHAR_CLASS)}]`,"g")))if(r+=o===t?1:-1,r<0)return n;return r>0?t:""}function ie(e,t,n){let r=e.replace(/\\./gsu,"");if(r.endsWith("\\"))return"\\";if(t===f.DEFAULT)return ne(r,"(",")");if(t===f.CHAR_CLASS&&!(n===d.ENCLOSED_TOKEN||n===d.Q_TOKEN))return ne(r,"[","]");if(t===f.ENCLOSED_TOKEN||t===f.INTERVAL_QUANTIFIER||n===d.ENCLOSED_TOKEN||n===d.Q_TOKEN){if(r.includes("}"))return"}"}else if(t===f.GROUP_NAME&&r.includes(">"))return">";return""}var re=new RegExp(String.raw`
(?<groupN>\(\?<(?![=!])|\\[gk]<)

@@ -75,3 +75,3 @@ | (?<enclosedT>\\[pPu]\{)

| \\?.
`.replace(/\s+/g,""),"gsu");function we(e){let t='Invalid unescaped "-" in character class',n=!1,r="";for(let{0:o,groups:{dp:i,vOnlyEscape:s}}of e.matchAll(Qe)){if(o[0]==="["){if(n)throw new Error("Invalid nested character class when flag v not supported; possibly from interpolation");if(o.endsWith("-"))throw new Error(t);n=!0}else if(o.endsWith("]")){if(o[0]==="-")throw new Error(t);n=!1}else if(n){if(o==="&&"||o==="--")throw new Error(`Invalid set operator "${o}" when flag v not supported`);if(i)throw new Error(`Invalid double punctuator "${o}", reserved by flag v`);if("(){}/|".includes(o))throw new Error(`Invalid unescaped "${o}" in character class`);if(s){r+=s;continue}}r+=o}return r}var je=(e,...t)=>{if(Array.isArray(e?.raw))return ee({},e,...t);if((typeof e=="string"||e===void 0)&&!t.length)return ee.bind(null,{flags:e??""});if({}.toString.call(e)==="[object Object]"&&!t.length)return ee.bind(null,e);throw new Error(`Unexpected arguments: ${JSON.stringify([e,...t])}`)},ee=(e,t,...n)=>{let r=Se(e),o=Te(t,n,r),i=0,s="",a;if(o.template.raw.forEach((u,c)=>{let p=!!(o.template.raw[c]||o.template.raw[c+1]);i+=U(u),s+=G(u,E.CHAR_CLASS),a=D(s,a);let{regexContext:g,charClassContext:l}=a;if(c<o.template.raw.length-1){let m=o.substitutions[c];s+=He(m,r.flags,g,l,p,i),m instanceof RegExp?i+=U(m.source):m instanceof $&&(i+=U(String(m)))}}),s=$e(s,r),r.subclass){let u=Be(s);return new te(u.expression,r.flags,{captureMap:u.captureMap})}return new RegExp(s,r.flags)};function qe(e="",t={}){let n=Se(t);if(n.subclass)throw new Error("Option subclass not supported");return{expression:$e(Te({raw:[e]},[],n).template.raw[0],n),flags:n.flags}}function Se(e){let t={flags:"",subclass:!1,plugins:[],unicodeSetsPlugin:we,disable:{},force:{},...e};if(/[nuvx]/.test(t.flags))throw new Error("Implicit flags v/u/x/n cannot be explicitly added");let n=t.force.v||(t.disable.v?!1:oe);return t.flags+=n?"v":"u",t}function Te(e,t,n){let r=[];n.disable.x||r.push(fe),n.disable.n||r.push(le);for(let o of r)({template:e,substitutions:t}=ue(e,t,o,n));return{template:e,substitutions:t}}function $e(e,t){let{flags:n,plugins:r,unicodeSetsPlugin:o,disable:i,subclass:s}=t;return[...r,...i.subroutines?[]:[Ne],...i.atomic?[]:[me,de],...i.x?[]:[pe],...!o||n.includes("v")?[]:[o]].forEach(a=>e=a(e,{flags:n,useEmulationGroups:s})),e}var te=class e extends RegExp{#e;constructor(t,n,r){super(t,n),r?this.#e=r.captureMap:t instanceof e&&(this.#e=t.#e)}exec(t){let n=RegExp.prototype.exec.call(this,t);if(!n||!this.#e)return n;let r=[...n];n.length=1;for(let o=1;o<r.length;o++)this.#e[o]&&n.push(r[o]);return n}};function He(e,t,n,r,o,i){if(e instanceof RegExp&&n!==f.DEFAULT)throw new Error("Cannot interpolate a RegExp at this position because the syntax context does not match");if(n===f.INVALID_INCOMPLETE_TOKEN||r===d.INVALID_INCOMPLETE_TOKEN)throw new Error("Interpolation preceded by invalid incomplete token");let s=e instanceof $,a="";if(!(e instanceof RegExp)){e=String(e),s||(a=B(e,n===f.CHAR_CLASS?E.CHAR_CLASS:E.DEFAULT));let u=ie(a||e,n,r);if(u)throw new Error(`Unescaped stray "${u}" in the interpolated value would have side effects outside it`)}if(n===f.ENCLOSED_TOKEN||n===f.INTERVAL_QUANTIFIER||n===f.GROUP_NAME||r===d.ENCLOSED_TOKEN||r===d.Q_TOKEN)return s?String(e):a;if(n===f.CHAR_CLASS){if(s){if(v(String(e),"^-|^&&|-$|&&$"))throw new Error("Cannot use range or set operator at boundary of interpolated pattern; move the operation into the pattern or the operator outside of it");let u=se(j(e));return z(e)?`[${u}]`:G(u)}return z(a)?`[${a}]`:a}if(e instanceof RegExp){let u=We(e,t),c=ae(u.value,i);return u.usedModifier?c:`(?:${c})`}return s?`(?:${e})`:o?`(?:${a})`:a}function We(e,t){let n={i:null,m:null,s:null},r="\\n\\r\\u2028\\u2029",o=e.source;if(e.ignoreCase!==t.includes("i"))if(x)n.i=e.ignoreCase;else throw new Error("Pattern modifiers not supported, so flag i on the outer and interpolated regex must match");if(e.dotAll!==t.includes("s")&&(x?n.s=e.dotAll:o=N(o,"\\.",e.dotAll?"[^]":`[^${r}]`,E.DEFAULT)),e.multiline!==t.includes("m")&&(x?n.m=e.multiline:(o=N(o,"\\^",e.multiline?`(?<=^|[${r}])`:"(?<![^])",E.DEFAULT),o=N(o,"\\$",e.multiline?`(?=$|[${r}])`:"(?![^])",E.DEFAULT))),x){let i=Object.keys(n),s=i.filter(u=>n[u]===!0).join(""),a=i.filter(u=>n[u]===!1).join("");if(a&&(s+=`-${a}`),s)return{value:`(?${s}:${o})`,usedModifier:!0}}return{value:o}}function Be(e){let t=C.replace(/\$/g,"\\$"),n=[!0];return e=N(e,`(?:${O})(?<mark>${t})?`,({0:r,groups:{mark:o}})=>o?(n.push(!1),r.slice(0,-C.length)):(n.push(!0),r),E.DEFAULT),{captureMap:n,expression:e}}return Oe(ze);})();
`.replace(/\s+/g,""),"gsu");function we(e){let t='Invalid unescaped "-" in character class',n=!1,r="";for(let{0:o,groups:{dp:i,vOnlyEscape:s}}of e.matchAll(Qe)){if(o[0]==="["){if(n)throw new Error("Invalid nested character class when flag v not supported; possibly from interpolation");if(o.endsWith("-"))throw new Error(t);n=!0}else if(o.endsWith("]")){if(o[0]==="-")throw new Error(t);n=!1}else if(n){if(o==="&&"||o==="--")throw new Error(`Invalid set operator "${o}" when flag v not supported`);if(i)throw new Error(`Invalid double punctuator "${o}", reserved by flag v`);if("(){}/|".includes(o))throw new Error(`Invalid unescaped "${o}" in character class`);if(s){r+=s;continue}}r+=o}return r}var je=(e,...t)=>{if(Array.isArray(e?.raw))return ee({},e,...t);if((typeof e=="string"||e===void 0)&&!t.length)return ee.bind(null,{flags:e??""});if({}.toString.call(e)==="[object Object]"&&!t.length)return ee.bind(null,e);throw new Error(`Unexpected arguments: ${JSON.stringify([e,...t])}`)},ee=(e,t,...n)=>{let r=Se(e),o=Te(t,n,r),i=0,s="",a;if(o.template.raw.forEach((u,c)=>{let p=!!(o.template.raw[c]||o.template.raw[c+1]);i+=U(u),s+=G(u,E.CHAR_CLASS),a=D(s,a);let{regexContext:g,charClassContext:l}=a;if(c<o.template.raw.length-1){let m=o.substitutions[c];s+=He(m,r.flags,g,l,p,i),m instanceof RegExp?i+=U(m.source):m instanceof $&&(i+=U(String(m)))}}),s=$e(s,r),r.subclass){let u=Be(s);return new te(u.expression,r.flags,{captureMap:u.captureMap})}return new RegExp(s,r.flags)};function qe(e,t={}){let n=Se(t);if(n.subclass)throw new Error("Cannot use option subclass");return e===void 0&&(e=""),{expression:$e(Te({raw:[e]},[],n).template.raw[0],n),flags:n.flags}}function Se(e){let t={flags:"",subclass:!1,plugins:[],unicodeSetsPlugin:we,disable:{},force:{},...e};if(/[nuvx]/.test(t.flags))throw new Error("Implicit flags v/u/x/n cannot be explicitly added");let n=t.force.v||(t.disable.v?!1:oe);return t.flags+=n?"v":"u",t}function Te(e,t,n){let r=[];n.disable.x||r.push(fe),n.disable.n||r.push(le);for(let o of r)({template:e,substitutions:t}=ue(e,t,o,n));return{template:e,substitutions:t}}function $e(e,t){let{flags:n,plugins:r,unicodeSetsPlugin:o,disable:i,subclass:s}=t;return[...r,...i.subroutines?[]:[Ne],...i.atomic?[]:[me,de],...i.x?[]:[pe],...!o||n.includes("v")?[]:[o]].forEach(a=>e=a(e,{flags:n,useEmulationGroups:s})),e}var te=class e extends RegExp{#e;constructor(t,n,r){super(t,n),r?this.#e=r.captureMap:t instanceof e&&(this.#e=t.#e)}exec(t){let n=RegExp.prototype.exec.call(this,t);if(!n||!this.#e)return n;let r=[...n];n.length=1;for(let o=1;o<r.length;o++)this.#e[o]&&n.push(r[o]);return n}};function He(e,t,n,r,o,i){if(e instanceof RegExp&&n!==f.DEFAULT)throw new Error("Cannot interpolate a RegExp at this position because the syntax context does not match");if(n===f.INVALID_INCOMPLETE_TOKEN||r===d.INVALID_INCOMPLETE_TOKEN)throw new Error("Interpolation preceded by invalid incomplete token");let s=e instanceof $,a="";if(!(e instanceof RegExp)){e=String(e),s||(a=B(e,n===f.CHAR_CLASS?E.CHAR_CLASS:E.DEFAULT));let u=ie(a||e,n,r);if(u)throw new Error(`Unescaped stray "${u}" in the interpolated value would have side effects outside it`)}if(n===f.ENCLOSED_TOKEN||n===f.INTERVAL_QUANTIFIER||n===f.GROUP_NAME||r===d.ENCLOSED_TOKEN||r===d.Q_TOKEN)return s?String(e):a;if(n===f.CHAR_CLASS){if(s){if(v(String(e),"^-|^&&|-$|&&$"))throw new Error("Cannot use range or set operator at boundary of interpolated pattern; move the operation into the pattern or the operator outside of it");let u=se(j(e));return z(e)?`[${u}]`:G(u)}return z(a)?`[${a}]`:a}if(e instanceof RegExp){let u=We(e,t),c=ae(u.value,i);return u.usedModifier?c:`(?:${c})`}return s?`(?:${e})`:o?`(?:${a})`:a}function We(e,t){let n={i:null,m:null,s:null},r="\\n\\r\\u2028\\u2029",o=e.source;if(e.ignoreCase!==t.includes("i"))if(x)n.i=e.ignoreCase;else throw new Error("Pattern modifiers not supported, so flag i on the outer and interpolated regex must match");if(e.dotAll!==t.includes("s")&&(x?n.s=e.dotAll:o=N(o,"\\.",e.dotAll?"[^]":`[^${r}]`,E.DEFAULT)),e.multiline!==t.includes("m")&&(x?n.m=e.multiline:(o=N(o,"\\^",e.multiline?`(?<=^|[${r}])`:"(?<![^])",E.DEFAULT),o=N(o,"\\$",e.multiline?`(?=$|[${r}])`:"(?![^])",E.DEFAULT))),x){let i=Object.keys(n),s=i.filter(u=>n[u]===!0).join(""),a=i.filter(u=>n[u]===!1).join("");if(a&&(s+=`-${a}`),s)return{value:`(?${s}:${o})`,usedModifier:!0}}return{value:o}}function Be(e){let t=C.replace(/\$/g,"\\$"),n=[!0];return e=N(e,`(?:${O})(?<mark>${t})?`,({0:r,groups:{mark:o}})=>o?(n.push(!1),r.slice(0,-C.length)):(n.push(!0),r),E.DEFAULT),{captureMap:n,expression:e}}return Oe(ze);})();
//# sourceMappingURL=regex.min.js.map
{
"name": "regex",
"version": "4.2.0",
"version": "4.2.1",
"description": "Regex template tag with extended syntax, context-aware interpolation, and always-on best practices",

@@ -5,0 +5,0 @@ "author": "Steven Levithan",

@@ -121,3 +121,3 @@ <div align="center">

<script type="module">
import {regex, pattern} from 'https://cdn.jsdelivr.net/npm/regex@4.2.0/+esm';
import {regex, pattern} from 'https://cdn.jsdelivr.net/npm/regex@4.2.1/+esm';
// …

@@ -131,3 +131,3 @@ </script>

```html
<script src="https://cdn.jsdelivr.net/npm/regex@4.2.0/dist/regex.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/regex@4.2.1/dist/regex.min.js"></script>
<script>

@@ -756,12 +756,16 @@ const {regex, pattern} = Regex;

For tools that want processed output as a string rather than a regex, you can use function `processRegex`:
### Returning a string
Function `rewrite` returns an object with properties `expression` and `flags` as strings, rather than returning a `RegExp` instance. This can be useful for tools that want to process the output.
```js
import {processRegex} from 'regex';
processRegex('^ . $', {flags: 'g'});
// β†’ {expression: '^.$', flags: 'gv'}
import {rewrite} from 'regex';
rewrite('^ (ab | cd)', {flags: 'm'});
// β†’ {expression: '^(?:ab|cd)', flags: 'mv'}
```
`processRegex` shares `regex`'s options (described above). Passing the resulting `expression` and `flags` properties to the `RegExp` constructor gives the same result as a tagged `regex` template. However, since `processRegex` isn't a template tag, it doesn't provide context-aware interpolation, and it doesn't automatically handle input as a raw string (so you need to escape your backslashes).
`rewrite` shares all of `regex`'s options (described above) except `subclass`. Providing the resulting `expression` and `flags` to the `RegExp` constructor produces the same result as using the `regex` tag.
> Since `rewrite` isn't a template tag, it doesn't provide context-aware interpolation and doesn't automatically handle input as a raw string (you need to escape your backslashes).
## ⚑ Performance

@@ -775,7 +779,7 @@

`regex` uses flag <kbd>v</kbd> (`unicodeSets`) when it's supported natively. Flag <kbd>v</kbd> is supported by 2023-era browsers ([compat table](https://caniuse.com/mdn-javascript_builtins_regexp_unicodesets)) and Node.js 20. When <kbd>v</kbd> isn't available, flag <kbd>u</kbd> is automatically used instead (while still enforcing <kbd>v</kbd>'s escaping rules), which extends support to Node.js 14 and 2020-era browsers (2017-era with a build step that transpiles private class fields, string `matchAll`, array `flatMap`, and the `??` and `?.` operators).
`regex` uses flag <kbd>v</kbd> (`unicodeSets`) when it's supported natively. Flag <kbd>v</kbd> is supported by Node.js 20 and 2023-era browsers ([compat table](https://caniuse.com/mdn-javascript_builtins_regexp_unicodesets)). When <kbd>v</kbd> isn't available, flag <kbd>u</kbd> is automatically used instead while enforcing <kbd>v</kbd>'s escaping rules, which extends support to Node.js 14 and 2020-era browsers.
The following edge cases rely on modern JavaScript features:
- To ensure atomization, `regex` uses nested character classes (which require flag <kbd>v</kbd>) when interpolating more than one token at a time *inside character classes*. A descriptive error is thrown when this isn't supported, which you can avoid by not interpolating multi-token patterns or strings into character classes. There's also an easy workaround: Put the whole character class in a `pattern` and interpolate a string into the pattern.
- To ensure atomization, `regex` uses nested character classes (which require flag <kbd>v</kbd>) when interpolating more than one token at a time *inside character classes*. A descriptive error is thrown when this isn't supported, which you can avoid by not interpolating multi-token patterns or strings into character classes. There's also an easy workaround: put the whole character class in a `pattern` and interpolate a string into the pattern.
- Using an interpolated `RegExp` instance with a different value for flag <kbd>i</kbd> than its outer regex relies on [pattern modifiers](https://github.com/tc39/proposal-regexp-modifiers), a bleeding-edge feature available in Chrome/Edge 125 and Opera 111. A descriptive error is thrown in environments without support, which you can avoid by aligning the use of flag <kbd>i</kbd> on inner and outer regexes. Local-only application of other flags doesn't rely on this feature.

@@ -782,0 +786,0 @@

@@ -87,3 +87,3 @@ export type InterpolatedValue = string | RegExp | Pattern | number;

*/
export function processRegex(expression?: string, options?: RegexTagOptions): {
export function rewrite(expression: string, options?: RegexTagOptions): {
expression: string;

@@ -104,2 +104,2 @@ flags: string;

}
export { pattern };
export { pattern, rewrite as processRegex };

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc