Skip to content

Commit

Permalink
Demo: toRegExp > compile
Browse files Browse the repository at this point in the history
  • Loading branch information
slevithan committed Oct 25, 2024
1 parent ecb7eb0 commit 8a4d7a8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 4 additions & 2 deletions demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ function showOutput(el) {
outputEl.classList.remove('error');
let output = '';
try {
const re = toRegExp(input, flags, {
// Don't actually run `toRegExp` in case the selected `target` includes features that don't
// work in the user's browser
const re = compile(input, flags, {
allowBestEffort: optionAllowBestEffortValue,
maxRecursionDepth: optionMaxRecursionDepthValue === '' ? null : +optionMaxRecursionDepthValue,
optimize: optionOptimizeValue,
target: optionTargetValue,
});
output = String(re);
output = `/${re.pattern ? re.pattern : '(?:)'}/${re.flags}`;
} catch (e) {
outputEl.classList.add('error');
output = `Error: ${e.message}`;
Expand Down
4 changes: 2 additions & 2 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ <h2>Try it</h2>
</p>
</details>
<pre id="output"></pre>
<p><small>The output shows the result of calling <code>toRegExp</code>. Oniguruma-To-ES includes functions to generate additional formats: <code>compile</code> (returns an object with <code>pattern</code> and <code>flags</code> strings), <code>toOnigurumaAst</code>, and <code>toRegexAst</code> (for an AST based on <a href="https://github.com/slevithan/regex"><code>regex</code></a>). You can run all of these from the console on this page. <code>compile</code> and <code>toRegExp</code> accept pattern and flags strings and an options object. <code>toOnigurumaAst</code> and <code>toRegexAst</code> accept a pattern and flags. You can also pass ASTs to <code>printAst</code>.</small></p>
<p><small>The output shows the result of calling <code>toRegExp</code>. Oniguruma-To-ES includes functions to generate additional formats: <code>compile</code> (returns an object with <code>pattern</code> and <code>flags</code> strings), <code>toOnigurumaAst</code>, and <code>toRegexAst</code> (for an AST based on <a href="https://github.com/slevithan/regex"><code>regex</code></a>). You can run all of these from the console on this page. <code>compile</code> and <code>toRegExp</code> accept pattern and flags strings and an options object. <code>toOnigurumaAst</code> and <code>toRegexAst</code> accept a pattern and flags. You can also pass AST results to <code>printAst</code>.</small></p>
</main>

<script src="../dist/index.min.js"></script>
<script>
Object.assign(globalThis, OnigurumaToES);

// For testing in the console
function printAst(ast) {
if (ast?.type !== 'Regex') {
throw new Error('Oniguruma or `regex` AST expected');
Expand Down

0 comments on commit 8a4d7a8

Please sign in to comment.