Regular expression Grammar & Collection
Regular expression
A regular expression is a text pattern that includes ordinary characters (for example, letters between an and z) and special characters (called metacharacters). The pattern describes one or more strings to match when searching for text.
Regular expression sample expression matching
/ ^\ sstories /
Matches a blank line.
/\ d {2} -\ d {5} /
Verify the ID number consisting of two digits, a hyphen, and five digits.
/] *)? > [\ s\ S] * /
Matches the HTML tag.
The following table contains a complete list of metacharacters and their behavior in the context of regular expressions:
Character description
\
Marks the next character as a special character, text, backreference, or octal escape character. For example, "n" matches the character "n". "\ n" matches the newline character. Sequence "\" match "," ("match" ().
^
Matches the position where the input string begins. If the Multiline property of the RegExp object is set, ^ also matches the position after "\ n" or "\ r".
$
Matches the position at the end of the input string. If the Multiline property of the RegExp object is set, $also matches the position before "\ n" or "\ r".
*
Matches the previous character or subexpression zero or more times. For example, zo* matches "z" and "zoo". * equivalent to {0,}.
+
Matches the previous character or subexpression one or more times. For example, "zo+" matches "zo" and "zoo" but not "z". + is equivalent to {1,}.
?
Matches the previous character or subexpression zero or once. For example, "do (es)?" Matches the do in "do" or "does". ? It is equivalent to {0jue 1}.
{n}
N is a non-negative integer. It happens to match n times. For example, "o {2}" does not match "o" in "Bob", but matches two "o" in "food".
{n,}
N is a non-negative integer. Match at least n times. For example, "o {2,}" does not match "o" in "Bob", but matches all o in "foooood". "o {1,}" is equivalent to "o +". "o {0,}" is equivalent to "o *".
{n,m}
M and n are non-negative integers, where n