C # commonly used in regular expressions Summary (1)

-

C # regular expressions in common summary

  Enter numbers only: "^ [0-9] * $."

  Only n-bit digital input: "^ \ d {n} $".

  Only input digital bits at least n: "^ \ d {n,} $".

  M ~ n can only enter the number of bits: "^ \ D {m, n} $"

  You can only enter a zero and non-zero number at the beginning: "^ (0 | [1-9] [0-9] *) $".

  There are only two positive real number input decimal: "(. [0-9] {2})? ^ [0-9] + $".

  1 to enter only three decimal positive real number: "(. [0-9] {1,3})? ^ [0-9] + $".

  You can only enter a non-zero positive integer: "? ^ \ + [1-9] [0-9] * $."

  You can only enter a non-zero negative integers: "^ \ - [1-9] [] 0-9" * $.

  Only 3 of the length of the input characters:. "$ ^ {3}."

  26 only by the input string of letters in English: "^ [A-Za-z] + $".

  You can only enter a string of 26 English capital letters: "^ [AZ] + $".

  You can only enter letters written by a string consisting of 26 small: "^ [az] + $".

  Only input string of numbers and English letters 26: "^ [A-Za-z0-9] + $".

  You can only enter the numbers, 26 English letters or underscore the string: "^ \ w + $".

  Verify User Password: "^ [a-zA-Z] \ w {5,17} $" The correct format is: begin with a letter, a length between 6 and 18, only contain characters, numbers and underscores.

  Verify that contain ^% & '?,; = $ \ "Characters such as:" [^% &'?,; = $ \ X22] + ".

  Can input Chinese characters: "^ [\ u4e00- \ u9fa5] {0,} $"

  验证Email地址:"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$"。

  验证InternetURL:"^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$"。

  Verify that the phone number: "^ (\ (\ d {3,4} -) | \ d {3.4} -) \ d {7,8} $?" The correct format is: "XXX-XXXXXXX", "XXXX-XXXXXXXX "," XXX-XXXXXXX "," XXX-XXXXXXXX "," XXXXXXX "and" XXXXXXXX ".

  ID verification number (15 or 18 digits): "^ \ d {15} | \ d {18} $".

  Verify year 12 months: "^ (0 [1-9] | 1 [0-2]?) $" Correct format: "01" through "09" and "1" to "12."

  Verify month 31 days: "^ (? (0 [1-9]) | ((1 | 2) [0-9]) | 30 | 31) $" correct format; "01" through "09" and "1" to "31."

  Limit the use of regular expressions in web form text input box contents:

  As a regular expression can only enter Chinese restrictions: onkeyup = "value = value.replace (/ [^ \ u4E00- \ u9FA5] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text'). replace (/ [^ \ u4E00- \ u9FA5] / g, '')) "

  As a regular expression can limit the full-size character input: onkeyup = "value = value.replace (/ [^ \ uFF00- \ uFFFF] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text'). replace (/ [^ \ uFF00- \ uFFFF] / g, '')) "

  As a regular expression can limit the input digital: onkeyup = "value = value.replace (/ [^ \ d] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text' ) .replace (/ [^ \ d] / g, '')) "

  As a regular expression can only enter numbers and restrictions: onkeyup = "value = value.replace (/ [\ W] / g, '')" onbeforepaste = "clipboardData.setData ( 'text', clipboardData.getData ( 'text ') .replace (/ [^ \ d] / g,' ')) "

  Have to use regular expressions to extract the file name from the URL address javascript program, the following results for the page1

 

The following is quoted fragment:
  S = "http://www.9499.net/page1.htm"
  S = s.replace does (/ (* \ /) {0,} ([^ \.] +) * /.. IG, "$ 2")
  Alert (S)

 

  Match double-byte characters (including Chinese characters included): [^ \ x00- \ xff]

  Application: calculate the length of the string (a double-byte character length meter 2, ASCII characters are counted 1)

 

The following is quoted fragment:
  String.prototype.len = function () {return this.replace ([^ \ x00- \ xFF] / G, "AA") length;.}

 

  Blank line matching the regular expression: \ n [\ s |] * \ r

  HTML tags matching regular expression:. / <(. *)> * <\ / \ 1> | <(. *) \ /> /

  Trailing spaces matching regular expression: (^ \ s *) | (\ s * $)

 

The following is quoted fragment:
  String.prototype.trim = function ()
  {
  return this.replace (/ (^ \ S *) | (\ $ * S) / G, "");
  }

 

  Use regular expressions decomposition and conversion IP address:

  The following regular expression matching using the IP address, and IP address values ​​into corresponding Javascript program:

 

The following is quoted fragment:
  function IP2V (IP)
  {
  ... Re = / (\ D +) \ (\ D +) \ (\ D +) \ (\ D +) / G // matches the IP address regular expression
  if (re .test (IP))
  {
  return the RegExp. $ *. 1 Math.pow (255,3)) + the RegExp. $ 2 * Math.pow (255,2)) + the RegExp. $. 3 * 255 + the RegExp. $ *. 1. 4
  }
  the else
  {
  new new Error the throw ( "Not Valid the IP address A!")
  }
  }
 

 

 

Reproduced in: https: //www.cnblogs.com/baishiying/archive/2012/09/26/2703482.html

Guess you like

Origin blog.csdn.net/weixin_33985507/article/details/93440048