bookmate game
en
Bøger
Loiane Groner

JavaScript Regular Expressions

  • Dannihar citeretfor 4 år siden
    This matches one or more occurrences and is equivalent to {1,}.
    /o+/ matches "oo" in "foo".
    ?
    This matches zero or one occurrences and is equivalent to {0,1}.
    /fo?/ matches "fo" in "foo" and matches "f" in "fairy".
    +?
    *?
    "?" can also be used following one of the *, +, ?, or {} quantifiers to make the later match nongreedy, or the minimum number of times versus the default maximum.
    /\d{2,4}?/ matches "12" in the "12345" string, instead of "1234" due to "?" at the end of the quantifier nongreedy.
    x(?=y)
    Positive lookahead: It matches x only if it's followed by y. Note that y is not included as part of the match, acting only as a required condition.
    /Java(?=Script|Hut)/ matches "Java" in "JavaScript" or "JavaHut", but not "JavaLand".
    x(?!y)
    Negative lookahead: It matches x only if it's not followed by y. Note that y is not included as part of the match, acting only as a required condition.
    /^\d+(?! years)/ matches "5" in "5 days" or "5 books", but not "5 years".
  • Dannihar citeretfor 4 år siden
    Quantifiers

    In the following table, you can find the patterns for quantifiers, which specify how many instances of a character, group, or character class must be present in an input for a match to be found.
    Pattern
    Description
    Example
    {n}
    This matches exactly n occurrences of a regular expression.
    /\d{5}/ matches "12345" (five digits) in "1234567890".
    {n,}
    This matches n or more occurrences of a regular expression.
    /\d{5,}/ matches "1234567890" (minimum of five digits) in "1234567890".
  • Dannihar citeretfor 4 år siden
    Character sets

    In the following table, you can find the patterns for character sets, which tell the Regex to match only one character out of several characters.
    Pattern
    Description
    Example
    [xyz]
    This matches any one character enclosed in the character set. You can use a hyphen to denote the range. For example, /[a-z]/ matches any letter in the alphabet and matches /[0-9]/ to any single digit.
    /[ao]/ matches "a" in "bar"
    [^xyz]
    This matches any one character, which is not enclosed in the character set.
    /[^ao]/ matches "b" in "bar"
  • Dannihar citeretfor 4 år siden
    javascript book/ matches "javascript book" in "javascript book"
    \0
    This matches a NUL character.

    \n
    This matches a newline character.

    \f
    This matches a form feed character.

    \r
    This matches a carriage return character.

    \t
    This matches a tab character.

    \v
    This matches a vertical tab character.

    [\b]
    This matches a backspace character.

    \xxx
    This matches the ASCII character, expressed by the xxx octal number.
    /112/ matches the "J" character
    \xdd
    This matches the ASCII character, expressed by the dd hex number.
    /x4A/ matches the "J" character
    \uxxxx
    This matches the ASCII character, expressed by the xxxx UNICODE.
    /u0237/ matches the "J" character
    \
    This indicates whether the next character is special and is not to be interpreted literally.
    /\^/ matches "^" in "char ^"
  • Dannihar citeretfor 4 år siden
    /\w+(?=\.com)/g
    The group with the ?= character will mean that we want it to have this text at the end of our pattern, but we don't actually want to include it; we also have to escape the period since it is a special character.
fb2epub
Træk og slip dine filer (ikke mere end 5 ad gangen)