Function charIn

  • Match a character listed in the group. A hyphen denotes a range of characters, such as a-z.

    Notes:

    charIn accepts a list of strings and special sequences, but you can also combine the list into one string if you prefer:

    • charIn('a-z0-9' + whitespace)
      
    • charIn`a-z0-9${whitespace}`
      
    • charIn`a-z0-9\s`
      

    However, combining a list of options into one string is not equivalent to a simple string concatenation. - is escaped at the beginning and end of each string in the list, so charIn`a-` `z` matches a, - and z literally, while charIn`a-z` matches alphabets from a to z.

    Apart from -, ^ and ] are also escaped in the character class, so you cannot negate a charIn via a ^ character (you should use notCharIn), and you cannot close a character class prematurely.

    Backslashes \ are only escaped at the end of a string, so you can use escape sequences such as \uffff and \xff freely. If you want to include `` in the character class, you should write it at the end of a string or escape with \\.

    Additionally, charIn allows you to merge character classes by simply passing in a charIn or charRange token. For example:

    const symbols = charIn`-_*$`; // the character class to be merged must not be negated (cannot be notCharIn or notCharRange)
    const alphabet = charRange`a``z`;
    const specialWord = charIn`0-9`(alphabet)(symbols);
    const specialWord2 = charIn`0-9${alphabet}${symbols}`;
    const notSpecialWord = notCharIn`0-9`(alphabet)(symbols);

    Parameters

    • template: TemplateStringsArray
    • Rest ...args: unknown[]

    Returns RegExpToken & CharClassFunction

    Example

    charIn`a-z_-`
    charIn('a-z', '_-')
    charIn`a-z``_-`

    RegExp equivalent:

    /[a-z_-]/
    

    Example

    Negated token

    notCharIn`a-z_-`
    not.charIn`a-z_-`
    notCharIn('a-z', '_-')
    notCharIn`a-z``_-`

    RegExp equivalent:

    /[^a-z_-]/
    
  • Match a character listed in the group. A hyphen denotes a range of characters, such as a-z.

    Notes:

    charIn accepts a list of strings and special sequences, but you can also combine the list into one string if you prefer:

    • charIn('a-z0-9' + whitespace)
      
    • charIn`a-z0-9${whitespace}`
      
    • charIn`a-z0-9\s`
      

    However, combining a list of options into one string is not equivalent to a simple string concatenation. - is escaped at the beginning and end of each string in the list, so charIn`a-` `z` matches a, - and z literally, while charIn`a-z` matches alphabets from a to z.

    Apart from -, ^ and ] are also escaped in the character class, so you cannot negate a charIn via a ^ character (you should use notCharIn), and you cannot close a character class prematurely.

    Backslashes \ are only escaped at the end of a string, so you can use escape sequences such as \uffff and \xff freely. If you want to include `` in the character class, you should write it at the end of a string or escape with \\.

    Additionally, charIn allows you to merge character classes by simply passing in a charIn or charRange token. For example:

    const symbols = charIn`-_*$`; // the character class to be merged must not be negated (cannot be notCharIn or notCharRange)
    const alphabet = charRange`a``z`;
    const specialWord = charIn`0-9`(alphabet)(symbols);
    const specialWord2 = charIn`0-9${alphabet}${symbols}`;
    const notSpecialWord = notCharIn`0-9`(alphabet)(symbols);

    Parameters

    Returns RegExpToken & CharClassFunction

    Example

    charIn`a-z_-`
    charIn('a-z', '_-')
    charIn`a-z``_-`

    RegExp equivalent:

    /[a-z_-]/
    

    Example

    Negated token

    notCharIn`a-z_-`
    not.charIn`a-z_-`
    notCharIn('a-z', '_-')
    notCharIn`a-z``_-`

    RegExp equivalent:

    /[^a-z_-]/
    

Generated using TypeDoc