0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

正規表現

Posted at

Basic Regular Expression

/abc/

In JS, regexes are strings that begin and end with /
The simplest regexes simply match a particular substring
The above regular expression matches any string containing "abc"
● Matches: "abc", "abcdef", "defabc", ".=.abc.=.", ...
● Doesn't match: "fedcba", "ab c", "PHP", ...

Wildcards, Case sensitivity

A . matches any character except a \n line break
● /.ax../ matches "Faxes", "Jaxes", "Taxes", "maxie", etc.

A trailing i at the end of a regex (after the closing /) signifies a case-insensitive
match
● /cal/i matches "Pascal", "California", "GCal", etc.

Quantifiers: *, +, ?

  • means 0 or more occurrences
    ● /abc*/ matches "ab", "abc", "abcc", "abccc", ...
    ● /a(bc)*/ matches "a", "abc", "abcbc", "abcbcbc", ...
    ● /a.*a/ matches "aa", "aba", "a8qa", "a!?xyz__9a", ...
  • means 1 or more occurrences
    ● /Hi!+ there/ matches "Hi! there", "Hi!!! there!", ...
    ● /a(bc)+/ matches "abc", "abcbc", "abcbcbc", ...
    ? means 0 or 1 occurrences
    ● /a(bc)?/ matches only "a" or "abc"

Character ranges: [start-end]

Inside a character set, specify a range of characters with -
● /[a-z]/ matches any lowercase letter
● /[a-zA-Z0-9]/ matches any lowercase or uppercase letter or digit
Inside a character set, - must be escaped to be matched
● /[+-]?[0-9]+/ matches an optional + or -, followed by at least one digit

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?