GithubNPM

API Guide

RXP Constructor

  • Creates a new RXP constructor from string, regex, or other RXP constructors passed as arguments. Provides a variety of methods to apply regex search conditions before being converted to a standard regex with the construct method.

    String arguments will automatically be escaped, while regex and RXP constructors are accepted as is.


    const sample = init("sample")
    sample.construct()  
    //  result: /sample/
    
    sample.occursBetween(3,5).and.atStart.construct()
    //  result: /^(?:(?:sample){3,5})/
    
    init(sample, / for /, "code example").construct("g")
    //  result: /sample for code example/g
  • Provides the current string version of the regex that will be formatted into a standard regex with the construct method.

    The text property is exposed for use in composing RXP constructors, but constructshould be used when actually formatting the regex.


    init("escape [ me").text
    //  result: "escape \\[ me"
  • Provides an alternative possible match for the initial search text. Every unique argument provided will result in an additional alternative possible text.


    sample.or('something else').construct() 
    //  result: /(?:(?:sample)|(?:something else))/
    
    sample.or("this", "that").construct()
    //  result: /(?:(?:sample)|(?:this)|(?:that))/
    
    
  • Transforms the RXP constructor into a standard regex, while correctly reconstructing variables. Regex flags can be specified as arguments passed to the function.


    sample.construct()
    //  result: /sample/
    
    sample.construct("g")
    // result: /sample/g
    
    sample.construct("global", "i", "s")
    // result: /sample/gis
  • Extends an RXP method to apply additional search conditions


    init("sample").occurs(3).and.atStart.construct() 
    //  result: /^(?:(?:sample){3})/

RXP Methods

Set Frequency

  • Define how many times the search text should occur, one after the other


    sample.occurs(3).construct() 
    //  result: /(?:sample){3}/
  • Marks the search text as occuring one or more times, one after another. A lazy search is used by default but can be converted to a greedy search with the follow-up .and.isGreedy method.


    sample.occursOnceOrMore.construct() 
    //  result: /(?:sample)+?/
  • Marks the search text as occuring zero or more times, one after another. A lazy search is used by default but can be converted to a greedy search with the follow-up .and.isGreedy method.


    sample.occursZeroOrMore.construct() 
    //  result: /(?:sample)*?/
  • Define the search text as repeating itself, one after another, a minimal number of times


    sample.occursAtLeast(2).construct() 
    //  result: /(?:sample){2,}/
  • Define the search text as repeating itself, one after another, between a minimal and maximal range


    sample.occursBetween( 2, 4).construct() 
    //  result: /(?:sample){2,4}/
  • Modifies previous search marker to use a greedy search. Can be applied after using either the occursOnceOrMore or occursZeroOrMore constructor methods, or after the oneOrMore or zeroOrMore shorthands.


    sample.occursOnceOrMore.and.isGreedy.construct() 
    //  result: /(?:sample)+/

Set Surroundings

  • Requires the search text to be followed by the text arguments passed to this method


    sample.followedBy("next").construct() 
    //  result: /sample(?=next)/
  • Requires the search text to NOT be followed by the text arguments passed to this method


    sample.notFollowedBy("nada").construct() 
    //  result: /sample(?!nada)/
  • Requires the search text to be preceded by the text arguments passed to this method


    sample.precededBy("before").construct() 
    //  result: /(?<=before)sample/
  • Requires the search text to NOT be preceded by the text arguments passed to this method


    sample.notPrecededBy("nada").construct() 
    //  result: /(?<!nada)sample/

Set Positioning

  • Requires search text to occur at the beginning of the string being tested


    sample.atStart.construct() 
    //  result: /^(?:sample)/
  • Requires search text to occur at the end of the string being tested


    sample.atEnd.construct() 
    //  result: /(?:sample)$/

Set Options

  • Marks search text as being optional. The text will not be required for a match but will be included when found


    sample.isOptional.construct() 
    //  result: /(?:sample)?/
  • Marks search text as being individually captured when found. By default, RXP uses noncapture groupings, but this method will override that behavior


    sample.isCaptured.construct() 
    //  result: /(sample)/
  • Marks the search text as a regex variable. This variable can then be used multiple times in a new RXP constructor.

    RXP will parse the variables when the construct method is called and reconfigure them based on the order of usage so that they will work as expected.

    A string argument can be provided to declare the variable name, or no argument can be provided and RXP will create a unique name for you.


    const RXPVar = init(/\d{3}/).isVariable("myVariable")
    init(RXPVar, " and ", RXPVar).construct() 
    //  result: /(?<myVariable>\d{3}) and \\k<myVariable>/
    //  the variable will be formatted based on its position in the regex

Presets

  • Matches any possible character. Equivalent to the . special character


    anyCharacter.construct() 
    //  result: /./
  • Matches any possible character except the ones passed as arguments to the function. Equivalent to negating charcters with the [^...] syntax.


    anyCharacterExcept("T", "x").construct() 
    //  result: /[^Tx]/
  • Matches any possible single digit. Equivalent to the \d special character.


    anyDigit.construct() 
    //  result: /[1234567890]/
  • Matches any possible single digit except those passed as arguments to the function


    anyDigitExcept(3,4,5).construct() 
    //  result: /[1267890]/
  • Matches any possible uppercase letter. Equivalent to [A-Z]


    anyUpperCase.construct() 
    //  result: /[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/
  • Matches any possible uppercase letter except those passed as arguments to the function


    anyUpperCaseExcept("A", "B", "C").construct() 
    //  result: /[DEFGHIJKLMNOPQRSTUVWXYZ]/
  • Matches any possible lowercase letter. Equivalent to [a-z].


    anyLowerCase.construct() 
    //  result: /[abcdefghijklmnopqrstuvwxyz]/
  • Matches any possible lowercase letter except those passed as arguments to the function


    anyLowerCaseExcept("x", "y", "z").construct() 
    //  result: /[abcdefghijklmnopqrstuvw]/
  • Matches any possible letter, regardless of case. Equivalent to [a-zA-Z].


    anyLetter.construct() 
    //  result: /[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ]/
  • Matches any possible lowercase or uppercase letter except those passed as arguments to the function


    anyLetterExcept("a", "A").construct() 
    //  result: [bcdefghijklmnopqrstuvwxyzBCDEFGHIJKLMNOPQRSTUVWXYZ]

Shorthands

  • Accepts two or more arguments that will be searched as alternate options. Equivalent to init("sample") .or("other").


    either("north", "south").construct() 
    //  result: /(?:(?:north)|(?:south))/
    
    const directions = ["up", "right", "down", "left"]
    either(...directions).construct()
    //  result: /(?:(?:up)|(?:right)|(?:down)|(?:left))/
  • Marks text as optional. Equivalent to init("sample") .isOptional.


    optional("maybe").construct() 
    //  result: /(?:maybe)?/
  • Marks search text as occuring once or more. Equivalent to init("sample") .occursOnceOrMore.


    oneOrMore("sample").construct() 
    //  result: /(?:sample)+?/
  • Marks search text as occuring zero or more times. Equivalent to init("sample") .occursZeroOrMore.


    zeroOrMore("sample").construct() 
    //  result: /(?:sample)*?/
  • Creates a new init-style function that will wrap any arguments provided to it in the arguments passed to wrapRXP


    const withBrackets = wrapRXP("[", "]")
    withBrackets("sample").construct() 
    //  result: /\[sample\]/
  • Provides an init-style function that automatically surrounds the search text with \b boundary markers


    withBoundaries("sample").construct() 
    // result: /\bsample\b/