Regular methods and commonly used regular

1. Regular writing:

var re = /a/; //Use literals, which have better performance than constructors (compile after loading scripts)
var re = new RegExp('a'); // use the constructor, (runtime compile) when you know the regexp pattern will change, or you don't know the pattern, and from another source, like user input.

2. Regular method:

method describe
exec A RegExp method that performs a search for a match in a string, returning an array (null if no match is found).
test A RegExp method that tests for a match in a string, returning true or false.
match A String method that performs a search for a match in a string, returning an array or null if no match is found.
search A String method that tests for a match in a string, returning the index of the position where the match was found, or -1 on failure.
replace A String method that performs a find match in a string and replaces the matched substring with the replacement string.
split A String method that separates a string using a regular expression or a fixed string and stores the separated substrings in an array.
(1) To know if a match in a string was found, use the test or search methods.
(2) For more information, etc., you can use the exec or match method. After the match is successful, the method will return an array and update the properties of the relevant regular expression object and the predefined regular expression object. If the match fails, then exec The method returns null (aka false).
(3) Regularly match the string, the successfully matched characters will be replaced with a new string, the usage method: string.replace (regular, new string), the second parameter can be a string or a callback function (Implicit loop, match to an execution callback function and then continue to match), the first parameter of the function is the successful matching character .

3. Regular and commonly used special characters:

character meaning
\

Matching will follow the following rules:

A backslash before a non-special character indicates that the next character is special and cannot be interpreted literally. For example, 'b' without a preceding '\' usually matches lowercase 'b' wherever they appear. If '\' is added, the character becomes a special character, meaning to match a character boundary .

A backslash can also escape special characters following it as literals . For example, the pattern /a*/ would match zero or more a's. In contrast, the pattern /a\*/ removes the specificity of '*' so that strings like "a*" can be matched.

Don't forget to escape \ when using new RegExp("pattern"), because \ is also an escape character inside a string.

^

Matches the start of the input . If the multiline flag is set to true, then the position immediately following a newline is also matched.

For example, /^A/ will not match the 'A' in "an A", but will match the 'A' in "An E".

$

Matches the end of the input . If multiline flag is set to true, also match the position before the newline.

For example, /t$/ will not match the 't' in "eater", but will match the 't' in "eat".

*

Matches the previous expression 0 or more times. Equivalent to {0,}.

For example, /bo*/ will match 'booooo' in "A ghost boooooed" and 'b' in "A bird warbled", but will match nothing in "A goat grunted".

+

匹配前面一个表达式1次或者多次。等价于 {1,}。

例如,/a+/匹配了在 "candy" 中的 'a',和在 "caaaaaaandy" 中所有的 'a'。

?

匹配前面一个表达式0次或者1次。等价于 {0,1}。

例如,/e?le?/ 匹配 "angel" 中的 'el',和 "angle" 中的 'le' 以及"oslo' 中的'l'。

如果紧跟在任何量词 *、 +、? 或 {} 的后面,将会使量词变为非贪婪的(匹配尽量少的字符),和缺省使用的贪婪模式(匹配尽可能多的字符)正好相反。

例如,对 "123abc" 应用 /\d+/ 将会返回 "123",如果使用 /\d+?/,那么就只会匹配到 "1"。

.

(小数点)匹配除换行符之外的任何单个字符。

例如,/.n/将会匹配 "nay, an apple is on the tree" 中的 'an' 和 'on',但是不会匹配 'nay'。

(x)

匹配 'x' 并且记住匹配项,就像下面的例子展示的那样。括号被称为 捕获括号

模式/(foo) (bar) \1 \2/中的 '(foo)' 和 '(bar)' 匹配并记住字符串 "foo bar foo bar" 中前两个单词。模式中的 \1 和 \2 匹配字符串的后两个单词。注意 \1、\2、\n 是用在正则表达式的匹配环节。在正则表达式的替换环节,则要使用像 $1、$2、$n 这样的语法,例如,'bar foo'.replace( /(...) (...)/, '$2 $1' )。

(?:x)

匹配 'x' 但是不记住匹配项。这种叫作非捕获括号,使得你能够定义为与正则表达式运算符一起使用的子表达式。来看示例表达式 /(?:foo){1,2}/。如果表达式是 /foo{1,2}/,{1,2}将只对 ‘foo’ 的最后一个字符 ’o‘ 生效。如果使用非捕获括号,则{1,2}会匹配整个 ‘foo’ 单词。

x(?=y)

匹配'x'仅仅当'x'后面跟着'y'.这种叫做正向肯定查找。

例如,/Jack(?=Sprat)/会匹配到'Jack'仅仅当它后面跟着'Sprat'。/Jack(?=Sprat|Frost)/匹配‘Jack’仅仅当它后面跟着'Sprat'或者是‘Frost’。但是‘Sprat’和‘Frost’都不是匹配结果的一部分。

x(?!y)

匹配'x'仅仅当'x'后面不跟着'y',这个叫做正向否定查找。

例如,/\d+(?!\.)/匹配一个数字仅仅当这个数字后面没有跟小数点的时候。正则表达式/\d+(?!\.)/.exec("3.141")匹配‘141’但是不是‘3.141’

x|y

匹配‘x’或者‘y’。

例如,/green|red/匹配“green apple”中的‘green’和“red apple”中的‘red’

{n}

n是一个正整数,匹配了前面一个字符刚好发生了n次。

比如,/a{2}/不会匹配“candy”中的'a',但是会匹配“caandy”中所有的a,以及“caaandy”中的前两个'a'。

{n,m}

n 和 m 都是整数。匹配前面的字符至少n次,最多m次。如果 n 或者 m 的值是0, 这个值被忽略。

例如,/a{1, 3}/ 并不匹配“cndy”中的任意字符,匹配“candy”中得a,匹配“caandy”中的前两个a,也匹配“caaaaaaandy”中的前三个a。注意,当匹配”caaaaaaandy“时,匹配的值是“aaa”,即使原始的字符串中有更多的a。

[xyz]

一个字符集合。匹配方括号的中任意字符,包括转义序列。你可以使用破折号(-)来指定一个字符范围。对于点(.)和星号(*)这样的特殊符号在一个字符集中没有特殊的意义。他们不必进行转义,不过转义也是起作用的。

例如,[abcd] 和[a-d]是一样的。他们都匹配"brisket"中得‘b’,也都匹配“city”中的‘c’。/[a-z.]+/ 和/[\w.]+/都匹配“test.i.ng”中的所有字符。

[^xyz]

一个反向字符集。也就是说, 它匹配任何没有包含在方括号中的字符。你可以使用破折号(-)来指定一个字符范围。任何普通字符在这里都是起作用的。

例如,[^abc] 和 [^a-c] 是一样的。他们匹配"brisket"中的‘r’,也匹配“chop”中的‘h’。

   
   
\d

匹配一个数字

等价于[0-9]

例如, /\d/ 或者 /[0-9]/ 匹配"B2 is the suite number."中的'2'。

\D

匹配一个非数字字符

等价于[^0-9]

例如, /\D/ 或者 /[^0-9]/ 匹配"B2 is the suite number."中的'B' 。

\f 匹配一个换页符 (U+000C)。
\n 匹配一个换行符 (U+000A)。
\r 匹配一个回车符 (U+000D)。
\s

匹配一个空白字符,包括空格、制表符、换页符和换行符

等价于[ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]。

例如, /\s\w*/ 匹配"foo bar."中的' bar'。

\S

匹配一个非空白字符。

等价于[^ \f\n\r\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]

例如, /\S\w*/ 匹配"foo bar."中的'foo'。

\t 匹配一个水平制表符 (U+0009)。
\v 匹配一个垂直制表符 (U+000B)。
\w

匹配一个单字字符(字母、数字或者下划线)。

等价于[A-Za-z0-9_]

例如, /\w/ 匹配 "apple," 中的 'a',"$5.28,"中的 '5' 和 "3D." 中的 '3'。

\W

匹配一个非单字字符。

等价于[^A-Za-z0-9_]

例如, /\W/ 或者 /[^A-Za-z0-9_]/ 匹配 "50%." 中的 '%'。

\n

In regular expressions, it returns the last n-th sub-capture of the matched substring (the number of captures is counted in the opening parenthesis).

For example  /apple(,)\sorange\1/ , match 'apple, orange,' in "apple, orange, cherry, peach.".

   

\n newline;
\s: space; \S: non-space
\d: number; \D: non-number
\w: character (including letters, numbers and underscore_)
\W: non-character

4. Commonly used rules:

  • Match Chinese: [\u4e00-\u9fa5]
  • Space at the beginning and end of the line: ^\s*|\s*$
  • Email : ^ \ w + @ [a-z0-9] + (\. [Az] +) {1,3} $
  • URL: [a-zA-z]+://[^\s]*
  • QQ number: [1-9][0-9]{4,9} The first digit is [1-9]
  • Postal Code: [1-9]\d{5}
  • ID card: [1-9]\d{14}|[1-9]\d{17}|[1-9]\d{16}x

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324682175&siteId=291194637