RegExp Flashcards
哪两种方法可以创建RegExp?
/pattern/flags;new RegExp(pattern [, flags]);
有哪些flags?
gglobal matchiignore casemmultiline; treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)ysticky; matches only from the index indicated by the lastIndex property of this regular expression in the target string (and does not attempt to match from any later indexes). This allows the match-only-at-start capabilities of the character “^” to effectively be used at any location in a string by changing the value of the lastIndex property.
matches any single character except the newline characters: \n \r \u2028 or \u2029.
.For example, /.y/ matches “my” and “ay”, but not “yes”, in “yes make my day”.
Matches a digit character in the basic Latin alphabet. Equivalent to [0-9].
\dFor example, /\d/ or /[0-9]/ matches ‘2’ in “B2 is the suite number.”
Matches any character that is not a digit in the basic Latin alphabet. Equivalent to [^0-9].
\DFor example, /\D/ or /[^0-9]/ matches ‘B’ in “B2 is the suite number.”
Matches any alphanumeric character from the basic Latin alphabet, including the underscore. Equivalent to [A-Za-z0-9_].
\wFor example, /\w/ matches ‘a’ in “apple,” ‘5’ in “$5.28,” and ‘3’ in “3D.”
Matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Za-z0-9_].
\WFor example, /\W/ or /[^A-Za-z0-9_]/ matches ‘%’ in “50%.”
Matches a single white space character, including space, tab, form feed, line feed and other unicode spaces. Equivalent to [ \t\r\n].
\sFor example, /\s\w*/ matches ‘ bar’ in “foo bar.”
Matches a single character other than white space (Not whitespace). Equivalent to [^ \t\r\n].
\SFor example, /\S\w*/ matches ‘foo’ in “foo bar.”
Matches a tab.
\t
Matches a carriage return.
\r
Matches a linefeed.
\n
Matches a backspace.
[\b] (Not to be confused with \b)
Where X is a letter from A - Z. Matches a control character in a string.
\cXFor example, /\cM/ matches control-M in a string.
将开头的字母与空格后的字母替换为大写。
(^|\s) ([a-z])String.prototype.capitalize = function(){ return this.replace( /(^|\s)([a-z])/g, function(m, p1, p2) { return p1+p2.toUpperCase(); });}