javascipt 正则表达式英文版
程序员文章站
2022-06-22 14:46:05
1. regular-expression literal characters character matches
alphanumeric character its...
1. regular-expression literal characters character matches
alphanumeric character itself
\0 the nul character (\u0000)
\t tab (\u0009)
\n newline (\u000a)
\v vertical tab (\u000b)
\f form feed (\u000c)
\r carriage return (\u000d)
\xnn the latin character specified by the hexadecimal number nn; for example, \x0a is the same as \n
\uxxxx the unicode character specified by the hexadecimal number xxxx; for example, \u0009 is the same as \t
\cx the control character ^x; for example, \cj is equivalent to the newline character \n
2. regular expression character classes character matches
[...] any one character between the brackets.
[^...] any one character not between the brackets.
. any character except newline or another unicode line terminator.
\w any ascii word character. equivalent to [a-za-z0-9_].
\w any character that is not an ascii word character. equivalent to [^a-za-z0-9_].
\s any unicode whitespace character.
\s any character that is not unicode whitespace. note that \w and \s are not the same thing.
\d any ascii digit. equivalent to [0-9].
\d any character other than an ascii digit. equivalent to [^0-9].
[\b] a literal backspace (special case).
3. regular expression repetition characters character meaning
{n,m} match the previous item at least n times but no more than m times.
{n,} match the previous item n or more times.
{n} match exactly n occurrences of the previous item.
? match zero or one occurrences of the previous item. that is, the previous item is optional. equivalent to {0,1}.
+ match one or more occurrences of the previous item. equivalent to {1,}.
* match zero or more occurrences of the previous item. equivalent to {0,}.
4。 regular expression alternation, grouping, and reference characters character meaning
| alternation. match either the subexpression to the left or the subexpression to the right.
(...) grouping. group items into a single unit that can be used with *, +, ?, |, and so on. also remember the characters that match this group for use with later references.
(?:...) grouping only. group items into a single unit, but do not remember the characters that match this group.
\n match the same characters that were matched when group number n was first matched. groups are subexpressions within (possibly nested) parentheses. group numbers are assigned by counting left parentheses from left to right. groups formed with (?: are not numbered.
5. regular-expression anchor characters character meaning
^ match the beginning of the string and, in multiline searches, the beginning of a line.
$ match the end of the string and, in multiline searches, the end of a line.
\b match a word boundary. that is, match the position between a \w character and a \w character or between a \w character and the beginning or end of a string. (note, however, that [\b] matches backspace.)
\b match a position that is not a word boundary.
(?=p) a positive lookahead assertion. require that the following characters match the pattern p, but do not include those characters in the match.
(?!p) a negative lookahead assertion. require that the following characters do not match the pattern p.
6. regular-expression flags character meaning
i perform case-insensitive matching.
g perform a global matchthat is, find all matches rather than stopping after the first match.
m multiline mode. ^ matches beginning of line or beginning of string, and $ matches end of line or end of string.
string.replace(regexp, replacement)
characters replacement
$1, $2, ..., $99 the text that matched the 1st through 99th parenthesized subexpression within regexp
$& the substring that matched regexp
$' the text to the left of the matched substring
$' the text to the right of the matched substring
$$ a literal dollar sign
name.replace(/(\w+)\s*,\s*(\w+)/, "$2 $1");
text.replace(/"([^"]*)"/g, "''$1''");
text.replace(/\b\w+\b/g, function(word) {
return word.substring(0,1).touppercase( ) +
word.substring(1);
});
上一篇: Java基础之选择结构与循环结构
下一篇: 正则表达式的使用 ASP