### Creating Semver RegExp with MagicRegExp (JavaScript) Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/3.examples.md This example demonstrates how to create a regular expression for parsing semver strings using the magic-regexp library. It uses functions like oneOrMore, digit, char, and maybe to define the structure of the semver pattern and groups to capture the major, minor, and patch versions. ```javascript import { char, createRegExp, digit, maybe, oneOrMore } from 'magic-regexp' createRegExp( oneOrMore(digit).groupedAs('major'), '.', oneOrMore(digit).groupedAs('minor'), maybe('.', oneOrMore(char).groupedAs('patch')) ) // /(?\d+)\.(?\d+)(?:\.(?.+))?/ ``` -------------------------------- ### Type-Level RegExp Match and Replace (TypeScript) Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/3.examples.md This example demonstrates the experimental type-level RegExp match and replace functionality of magic-regexp. It shows how to use createRegExp with literal strings to get type information about the match results, including groups and indices. It also demonstrates the replace functionality. ```typescript import { anyOf, createRegExp, digit, exactly, oneOrMore, wordChar } from 'magic-regexp/further-magic' const literalString = 'magic-regexp 3.2.5.beta.1 just release!' const semverRegExp = createRegExp( oneOrMore(digit) .as('major') .and('.') .and(oneOrMore(digit).as('minor')) .and( exactly('.') .and(oneOrMore(anyOf(wordChar, '.')).groupedAs('patch')) .optionally() ) ) // `String.match()` example const matchResult = literalString.match(semverRegExp) matchResult[0] // "3.2.5.beta.1" matchResult[3] // "5.beta.1" matchResult.length // 4 matchResult.index // 14 matchResult.groups // groups: { // major: "3"; // minor: "2"; // patch: "5.beta.1"; // } // `String.replace()` example const replaceResult = literalString.replace( semverRegExp, `minor version "$2" brings many great DX improvements, while patch "$" fix some bugs and it's` ) replaceResult // "magic-regexp minor version \"2\" brings many great DX improvements, while patch \"5.beta.1\" fix some bugs and it's just release!" ``` -------------------------------- ### Inspecting RegExp creation with exactly Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/2.usage.md This example demonstrates how to inspect the RegExp generated by the `exactly` helper. Hovering over the function calls will show the input and output regular expressions. ```typescript import { exactly } from 'magic-regexp' exactly('test.mjs') // (alias) exactly<"test.mjs">(input: "test.mjs"): Input<"test\\.mjs", never> exactly('test.mjs').or('something.else') // (property) Input<"test\\.mjs", never>.or: <"something.else">(input: "something.else") => Input<"(?:test\\.mjs|something\\.else)", never> ``` -------------------------------- ### Backreferences in RegExp with MagicRegExp (JavaScript) Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/3.examples.md This example shows how to use backreferences to previously captured groups in a regular expression created with magic-regexp. It defines a pattern that matches a string where the first and second characters are repeated at the end, demonstrating the use of .referenceTo(). ```javascript import assert from 'node:assert' import { char, createRegExp, oneOrMore, wordChar } from 'magic-regexp' const TENET_RE = createRegExp( wordChar .groupedAs('firstChar') .and(wordChar.groupedAs('secondChar')) .and(oneOrMore(char)) .and.referenceTo('secondChar') .and.referenceTo('firstChar') ) // /(?\w)(?\w).+\k\k/ assert.equal(TENET_RE.test('TEN<==O==>NET'), true) ``` -------------------------------- ### Importing from magic-regexp/further-magic Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/2.usage.md This example shows how to import helpers from the `magic-regexp/further-magic` subpath to use the experimental type-level matching feature. ```typescript import { createRegExp, digit, exactly } from 'magic-regexp/further-magic' ``` -------------------------------- ### Configuring MagicRegExp Transform Plugin with Unbuild Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/1.index.md This snippet shows how to integrate the MagicRegExpTransformPlugin with Unbuild. It configures the plugin within the Unbuild configuration file to enable build-time transforms. Add this to your build.config.ts file. ```javascript import { MagicRegExpTransformPlugin } from 'magic-regexp/transform' // unbuild import { defineBuildConfig } from 'unbuild' export default defineBuildConfig({ hooks: { 'rollup:options': (options, config) => { config.plugins.push(MagicRegExpTransformPlugin.rollup()) }, }, }) ``` -------------------------------- ### Matching Dynamic Strings with Type Inference (TypeScript) Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/3.examples.md This example shows how to use magic-regexp with dynamic strings and how the type system infers the possible match results. It demonstrates the use of .or() to create a regular expression that matches either 'foo' or 'bar', and shows how the match result type reflects the possible values. ```typescript const myString = 'dynamic' const RegExp = createRegExp(exactly('foo').or('bar').groupedAs('g1')) const matchAllResult = myString.match(RegExp) matchAllResult // null | RegExpMatchResult<{ // matched: ["bar", "bar"] | ["foo", "foo"]; // namedCaptures: ["g1", "bar"] | ["g1", "foo"]; // input: string; // restInput: undefined; // }> matchAllResult?.[0] // ['foo', 'foo'] | ['bar', 'bar'] matchAllResult?.length // 2 | undefined matchAllResult?.groups // groups: { // g1: "foo" | "bar"; // } | undefined ``` -------------------------------- ### Creating a Regular Expression with magic-regexp Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/2.usage.md This example demonstrates how to create a regular expression using the createRegExp function and the exactly and after helpers from the magic-regexp library. It imports the necessary functions, defines a pattern to match 'foo/test.js' after 'bar/', and logs the resulting regular expression. ```javascript import { createRegExp, exactly } from 'magic-regexp' const regExp = createRegExp(exactly('foo/test.js').after('bar/')) console.log(regExp) // /(?<=bar\/)foo\/test\.js/ ``` -------------------------------- ### Using MagicRegExp Transform Plugin with Vite Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/1.index.md This snippet shows how to integrate the MagicRegExpTransformPlugin with Vite. It configures the plugin within the Vite configuration file to enable build-time transforms. Add this to your vite.config.ts file. ```javascript import { MagicRegExpTransformPlugin } from 'magic-regexp/transform' import { defineConfig } from 'vite' export default defineConfig({ plugins: [MagicRegExpTransformPlugin.vite()], }) ``` -------------------------------- ### Type-level matching with literal strings Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/2.usage.md This example demonstrates type-level matching with a literal string. The type of the matched result, groups, index, and length are inferred. ```typescript 'foo'.match(createRegExp(exactly('foo').groupedAs('g1'))) ``` -------------------------------- ### Using createRegExp with different input types Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/2.usage.md This example showcases the flexibility of the createRegExp function by demonstrating its usage with various input types, including strings, Input objects created with helpers like exactly and maybe, and flag arrays. It illustrates how to combine these inputs to create complex regular expressions with specific flags. ```javascript import { createRegExp, exactly, global, maybe, multiline } from 'magic-regexp' createRegExp(exactly('foo').or('bar')) createRegExp('string-to-match', [global, multiline]) // you can also pass flags directly as strings or Sets createRegExp('string-to-match', ['g', 'm']) // or pass in multiple `string` and `input patterns`, // all inputs will be concatenated to one RegExp pattern createRegExp( 'foo', maybe('bar').groupedAs('g1'), 'baz', [global, multiline] ) // equivalent to /foo(?(?:bar)?)baz/gm ``` -------------------------------- ### Integrating MagicRegExp Transform Plugin in Next.js Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/1.index.md This snippet demonstrates how to integrate the MagicRegExpTransformPlugin into a Next.js project using the webpack configuration. It modifies the webpack configuration to include the plugin. Add this to your next.config.mjs file. ```javascript import { MagicRegExpTransformPlugin } from 'magic-regexp/transform' export default { webpack(config) { config.plugins = config.plugins || [] config.plugins.push(MagicRegExpTransformPlugin.webpack()) return config }, } ``` -------------------------------- ### Type-level matching with dynamic strings Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/2.usage.md This example demonstrates type-level matching with a dynamic string. The type of the matched result and groups are unions of possible matches. ```typescript myString.match(createRegExp(exactly('foo').or('bar').groupedAs('g1'))) ``` -------------------------------- ### Configuring MagicRegExp Module in Nuxt 3 Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/1.index.md This snippet demonstrates how to configure the magic-regexp module within a Nuxt 3 project. It enables auto-imports of magic-regexp helpers. Add this to your nuxt.config.ts file. ```javascript import { defineNuxtConfig } from 'nuxt' export default defineNuxtConfig({ // This will also enable auto-imports of magic-regexp helpers modules: ['magic-regexp/nuxt'], }) ``` -------------------------------- ### TypeScript Playground Example Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/index.md This TypeScript code snippet demonstrates the usage of magic-regexp. It showcases how to define and use regular expressions in a type-safe and readable manner within the TypeScript Playground environment. The code includes features like character classes, quantifiers, and capture groups. ```TypeScript JYWwDg9gTgLgBAbzgYwBYEMoBoVQKbox4BKeA5gKIAeYOAJsGcDDnlesjADYCeOI6HgCM8OCADs8AeSgBZaKLgB3aHQDCGKHAC+cAGZQIIOAHIBTZAFp8ZNmBMAoBwHoAVK4dxXKDONtwYCE9vUEhYRAA6KN0DI1NzYCsbO2c9AFdYVDwoSwTkRy8AiACoHgCeMDw4LjwANzwuOFJKGjgBGDQ4fABnNK4YbrgACjts0DxxGHQuAEpg5ydnZzgAFQq8OhR0MBgMqrJDNLBuh2QJbvgASQARAH1iCjgAXlwCImbqMBH2Tl4hk2AdEsJhmEXQ4joQwYTBgERg426QwArKCDhAjhsAIKIgF0EEzOZncQXOBoo6DF4mbpGKqAywAJgAzAAWACcAAY4Fl8Mp9hATBF2mghjd7hQZgB+CJk44uZZwBVwAB6EsWywAimlEgBrSzgoEMWBlbp4ED1KCnc7wADKFFkADUKMQxc9XoQSORPkNPHAJNI5AooYxmKjDpU6Nj-gIAFbQEFYH0CkwJhV+mTyfBBmGh9HhyNmYDiOMzFNtQQif4RZO+yTpwNoTA5jERnFgQhofEJuYOW0Op1iuXK1WW4nwHp9eAvZD4d0fGjfDjcHj-PQQCDOIgXCLR7ogsF6IhQf5CTDOfFwvAXY+n1frzewncgtVNPB6bITZCXopwMD4WrAdFul4LYdj2TYZUGNJukLMgAiyUkwzgcR0BAPARxJFYKAAOQoFYXSnGd3k9ecfTYRc-hUKB1E0aUwyxHE9GAKALg0TB42UVRWKgWjc3o-4TSJai2JLGt-QzPAhgbKACR9BUwQhCJ8DffBxE-FYIH4vBBK4p9FTgeS6EU1931UvB1JXJiWM0eMHG7IkSXHfp6VdTCcLwh4LyvExXIAHieJ4pH8gA+NyQSAA ``` -------------------------------- ### Transform RegExp with createRegExp Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/4.transform.md This example shows how to use the `createRegExp` function with the build-time transform to create a regular expression. The `exactly` and `after` helpers are used to define the pattern. The resulting regular expression is compiled at build time. ```javascript const beforeTransform = createRegExp(exactly('foo/test.js').after('bar/')) // => gets _compiled_ to const afterTransform = /(?<=bar\/)foo\/test\.js/ ``` -------------------------------- ### Converting Regular Expression to Magic-Regexp Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/5.converter.md Converts a regular expression to `magic-regexp` syntax using the `convert` function. The example demonstrates converting `/[abc]/` and `/(foo)bar\d+/` to their `magic-regexp` equivalents. ```typescript import { convert } from 'magic-regexp/converter' convert(/[abc]/) // createRegExp(exactly('a').or('b').or('c')) convert(/(foo)bar\d+/) // createRegExp(exactly('foo').grouped(), 'bar', oneOrMore(digit)) ``` -------------------------------- ### Create RegExp with Dynamic Variable Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/4.transform.md This example demonstrates the limitation of the build-time transform when using dynamic variables. Because `someString` is a variable, the regular expression cannot be compiled at build time, but it will still work with a minimal runtime. ```javascript const someString = 'test' const regExp = createRegExp(exactly(someString)) ``` -------------------------------- ### Converting Regular Expression with ArgsOnly Option Source: https://github.com/unjs/magic-regexp/blob/main/docs/content/1.guide/5.converter.md Converts a regular expression to `magic-regexp` syntax, displaying only the arguments without the `createRegExp` wrapper. The `argsOnly` option is set to `true`. ```typescript convert(/\w+@\w\.com/, { argsOnly: true }) // oneOrMore(wordChar), '@', wordChar, '.com' ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.