UNPKG

regexp-coder

Version:

A Javascript/Typescript RegExp Coder

486 lines 16.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RegExpSpec = void 0; /** * Regular Expression Specification * - Provide basic elements of Javascript/Type Regular Expression * @see [Regular Expressions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions) */ class RegExpSpec { /** * `\`, backslash. * @see [backslash](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-backslash) */ static get backslash() { return '\\'; } /** * `^`, * Matches beginning of input. * If the multiline flag is set to true, also matches immediately after a line break character. * * @see: [Caret](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-caret) */ static get begin() { return '^'; } /** * `$`, * Matches end of input. * If the multiline flag is set to true, also matches immediately before a line break character. * * @see [dollar](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-dollar) */ static get end() { return '$'; } /** * `*`, * Matches the preceding expression 0 or more times. Equivalent to `{0,}`. * * @see [asterisk](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-asterisk) */ static get more() { return this.asterisk; } /** * `*`, * Matches the preceding expression 0 or more times. Equivalent to `{0,}`. * * @see [asterisk](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-asterisk) */ static get asterisk() { return '*'; } /** * `+`, * Matches the preceding expression 1 or more times. Equivalent to `{1,}`. * * @see [plus](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-plus) */ static get oneOrMore() { return this.plus; } /** * `+`, * Matches the preceding expression 1 or more times. Equivalent to `{1,}`. * * @see [plus](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-plus) */ static get plus() { return '+'; } /** * `?` * Matches the preceding expression 0 or 1 time. Equivalent to `{0,1}`. * * @see [questionmark](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-questionmark) */ static get zeroOrOne() { return this.questionmark; } /** * `?`, * Matches the preceding expression 0 or 1 time. Equivalent to `{0,1}`. * * @see [questionmark](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-questionmark) */ static get questionmark() { return '?'; } /** * `.`, * Matches any single character except the newline character, by default. * * @see [dot](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-dot) */ static get any() { return this.dot; } /** * `.`, * Matches any single character except the newline character, by default. * * @see [dot](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-dot) */ static get dot() { return '.'; } /** * `(x)`, Matches `x` and remembers the match. * The parentheses are called capturing parentheses. * `(?<name>:x)`, Named capturing group: Matches x and stores it * on the groups property of the returned matches under the name specified by <Name>. * * @param x expression * @param name name * * @see [capturing parentheses](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-capturing-parentheses) * @see [Groups and Ranges](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions/Groups_and_Ranges) */ static capturingParentheses(x, name) { const source = typeof x === 'string' ? x : x.source; return `(${name ? `?<${name}>:` : ''}${source})`; } /** * `(?:x)`, Matches `x` but does not remember the match. * The parentheses are called non-capturing parentheses. * @param x expression * * @see [non capturing parentheses](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-non-capturing-parentheses) */ static nonCapturingParentheses(x) { const source = typeof x === 'string' ? x : x.source; return `(?:${source})`; } /** * `x(?=y)`, * Matches `x` only if `x` is followed by `y`. This is called a lookahead. * * @param x expression * @param followedBy the followed by expression * * @see [lookahead](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-lookahead) */ static lookahead(x, followedBy) { const source = typeof x === 'string' ? x : x.source; const ySource = typeof (followedBy) === 'string' ? followedBy : followedBy.source; return `${source}(?=${ySource})`; } /** * `x(?!y)` * Matches `x` only if `x` is not followed by `y`. * This is called a negated lookahead. * @param x expression * @param notFollowedBy the not followed by expression * * @see [negated look ahead](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-negated-look-ahead) */ static negatedLookahead(x, notFollowedBy) { const source = typeof x === 'string' ? x : x.source; const ySource = typeof (notFollowedBy) === 'string' ? notFollowedBy : notFollowedBy.source; return `${source}(?!${ySource})`; } /** * `(?<=y)x`, * Matches `x` only if `x` is preceded by `y`. * This is called a lookbehind. * * @param x expression * @param precededBy the preceded by expression * * @see [lookbehind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-lookbehind) */ static lookbehind(x, precededBy) { const source = typeof x === 'string' ? x : x.source; const ySource = typeof (precededBy) === 'string' ? precededBy : precededBy.source; return `(?<=${ySource})${source}`; } /** * `(?<!y)x`, * Matches `x` only if `x` is not preceded by `y`. * This is called a negated lookbehind. * * @param x expression * @param notPrecededBy the not preceded by expression * * @see [negative lookbehind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-negative-lookbehind) */ static negatedLookbehind(x, notPrecededBy) { const source = typeof x === 'string' ? x : x.source; const ySource = typeof (notPrecededBy) === 'string' ? notPrecededBy : notPrecededBy.source; return `(?<!${ySource})${source}`; } /** * `x|y`, * Matches 'x', or 'y' (if there is no match for 'x'). * * @param items expression * * @see [or](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-or) */ static or(...items) { let source = ''; items.forEach((value, index) => { source += (index === 0 ? '' : '|') + (typeof (value) === 'string' ? value : value.source); }); return source; } /** * `{n}`, * Matches exactly n occurrences of the preceding expression. N must be a positive integer. * * @param n times of occurrences * * @see [quantifier](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-quantifier) */ static occurrence(n) { return `{${n}}`; } /** * `(n, }`, `{n, m}`, * Matches at least `n` occurrences of the preceding expression. `N` must be a positive integer. * Where `n` and `m` are positive integers and `n <= m`. * Matches at least `n` and at most `m` occurrences of the preceding expression. * When `m` is omitted, it's treated as `∞`. * * @param n at least occurrences * @param m (optional) at most occurrences * * @see [quantifier](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-quantifier) * @see [quantifier-range](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-quantifier-range) */ static occurrenceOrMore(n, m) { return `{${n}, ${m || ''}}`; } /** * `[xyz]`, * Character set. * * @see [character set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-character-set) */ static characterSet(...chars) { const source = chars.join(''); return `[${source}]`; } /** * `[^xyz]`, * A negated or complemented character set. * * @see [negated character set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-negated-character-set) */ static negatedCharacterSet(...chars) { const source = chars.join(''); return `[^${source}]`; } /** * `[\b]`, * Matches a backspace (U+0008). * * @see [backspace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-backspace) */ static get backspace() { return '[\\b]'; } /** * `\b` * Matches a word boundary. * * @see [word boundary](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-word-boundary) */ static get wordBoundary() { return '\\b'; } /** * `\B`, Matches a non-word boundary. * * @see [non word boundary](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-non-word-boundary) */ static get nonWordBoundary() { return '\\B'; } /** * `\cX` * Matches a control character in a string. * * @param X a character ranging from A to Z. * * @see [control](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-control) */ static controlCharacter(X) { return `\\c${X}`; } /** * `\d`, Matches a digit character. Equivalent to `[0 - 9]`. * * @see [digit](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-digit) */ static get digit() { return '\\d'; } /** * `\D`, Matches a non-digit character. Equivalent to `[^ 0 - 9]`. * * @see [non digit](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-non-digit) */ static get nonDigit() { return '\\D'; } /** * `\f`, * Matches a form feed (U+000C). * * @see [form-feed](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-form-feed) */ static get formfeed() { return '\\f'; } /** * `\n`, * Matches a line feed (U+000A). * * @see [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-line-feed) */ static get linefeed() { return '\\n'; } /** * `\r`, * Matches a carriage return (U+000D). * * @see [](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-carriage-return) */ static get carriageReturn() { return '\\r'; } /** * `\s`, * Matches a white space character, including space, tab, form feed, line feed. * * @see [white space](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-white-space) */ static get whitespace() { return '\\s'; } /** * `\S`, * Matches a character other than white space. * * @see [non white space](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-non-white-space) */ static get nonWhitespace() { return '\\S'; } /** * `\t`, * Matches a tab (U+0009). * * @see [tab](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-tab) */ static get tab() { return '\\t'; } /** * `\v`, * Matches a vertical tab (U+000B). * * @see [vertical tab](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-vertical-tab) */ static get verticalTab() { return '\\v'; } /** * `\w`, * Matches any alphanumeric character including the underscore. * * @see [word](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-word) */ static get alphanumeric() { return '\\w'; } /** * `\W`, * Matches any non-word character. * * @see [non word](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-non-word) */ static get nonWord() { return '\\W'; } /** * `\n`, * Where `n` is a positive integer, * a back reference to the last substring matching the `n` parenthetical * in the regular expression (counting left parentheses). * * @see [backreference](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-backreference) */ static backreference(n) { return `\\${n}`; } /** * `\0`, Matches a NULL (U+0000) character. * * @see [null](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-null) */ static get null() { return '\\0'; } /** * `\xhh`, * Matches the character with the code `hh`. * @param hh two hexadecimal digits * * @see [hex escape](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-hex-escape) */ static hex(hh) { return `\\x${hh.padStart(2, '0')}`; } /** * `\uhhhh` * Matches the character with the code `hhhh`. * @param hhhh four hexadecimal digits * * @see [unicode-escape](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-unicode-escape) */ static unicode(hhhh) { return `\\u${hhhh.padStart(4, '0')}`; } /** * `\u{hhhh} ` * (only when u flag is set) * Matches the character with the Unicode value hhhh (hexadecimal digits). * @param hhhh four hexadecimal digits * * @see [unicode-escape-es6](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#special-unicode-escape-es6) */ static unicodeU(hhhh) { return `\\u{${hhhh.padStart(4, '0')}}`; } /** * `g`, * Global search. */ static get globalSearchFlag() { return 'g'; } /** * `i`, * Case-insensitive search. */ static get caseInsensitiveSearchFlag() { return 'i'; } /** * `n`, * Multi-line search. */ static get multipleLineSearchFlag() { return 'm'; } /** * `s`, * Allows . to match newline characters. */ static get dotIsNewLineFlag() { return 's'; } /** * `u`, * "unicode"; treat a pattern as a sequence of unicode code points. */ static get unicodeFlag() { return 'u'; } /** * `y`, * Perform a "sticky" search that matches starting at the current position in the target string. * * @see [sticky](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky) */ static get strickySearchFlag() { return 'y'; } } exports.RegExpSpec = RegExpSpec; //# sourceMappingURL=regExpSpec.js.map