Knowledge:

Regular Expressions Overview Regular Expressions Creation Content Meaning Common regular expressions in regular verification code JS

Overview of regular expressions

Regular expressions are patterns used to match combinations of strings in strings. In JS, Regular expressions are objects used to: create password submission forms, match strings, filter sensitive words, and extract specific strings. At present, the search front end mainly uses regular expressions to complete the verification of forms, which is generally speaking, matching information

There are two kinds of precise matching and fuzzy matching

Second, regular expression creation

Two ways:

Var RegExp = new RegExp (/123/)

2. Use regular expression arguments to create (the most common method) Var rg = /123/

Once created, you must check that the string conforms to the regular expression. Testing the regular expression text to see if characters conform to the regular expression returns true and false. The argument is the test string regexobj.test (STR) and STR is the content of the string that we wrote

Note: You are not allowed to enclose quotation marks in regular expressions, whether strings or numbersCopy the code

3. Specific meaning of regular expression content

Because I am also a beginner, as far as possible with popular language to write, did not use inverted professional noun, please forgive me.

Boundary characters (generally used for exact matching)

^ Start with it $end with it

Quantifier operator

* Repeat zero or more times + repeat one or more times? Repeat zero or once {n} repeat n times {n,} repeat n times or more {n, m} repeat n to m times

  • Or |

Parentheses summary

Bracket character summary matches any character inside square brackets [] curly bracket {} quantifier, which indicates the number of repetitions; The parentheses indicate the priority.

Common character abbreviation

\ D means [0-9] a digit

\D[^0-9] represents any character except digits.

\w[0-9A-zA-z]. Indicates numbers, upper and lower case letters, and underscores. How to remember: W is a short Word character \ w (uppercase W) [^ 0-9a-za-z]. Non-word characters.

\s is [\t\v\n\r\f]. Represents whitespace, including Spaces, horizontal tabs, vertical tabs, line feeds, carriage returns, and page feeds. How to remember: S is the first letter of space character. \S is [^ \t\v\n\r\f]. Non-whitespace character.

Is [^\n\r\u2028\u2029]. Wildcard character, representing almost any character. Newline, carriage return, line and segment separators are excluded. How to remember it: Think ellipses… Each of these dots can be interpreted as a placeholder for anything like it.

Fourth, the common re verification code

Verification letter: /^[A-ZA-Z]+$/ The verification length is3/^.{3} $/ verification by26A string of letters: /^[A-zA-z]+$/ Verification date YYYY-MM-DD: /^(\d{1.4})(-|\/)(\d{1.2The \})2(\d{1.2})$/ verify zip: /^\d{6}$/ verify date format YYYY-MM-DD hh: MM :ss: /^(\d{1.4})(-|\/)(\d{1.2The \})2(\d{1.2}) (\d{1.2}):(\d{1.2}):(\d{1.2})$/ validate integer: /^[-+]? \d*$/ verify decimal: /^[-\+]? \d+(\.\d+)? Chinese: $/ verification / ^ [\ u0391 - \ uFFE5] + $/ verification email: / ^ \ w + (\ w + / - +.]) * @ \ w + ([-] \ w +) * \ \ w + ([-] \ w +) * $/ verify phone number: / ^1[3456789]\d{9}$/ verify id: /^\d{6} (18|19|20)? \d{2} (0[1-9] |1[12]) (0[1-9"|"12]\d|3[01])\d{3}(\d|X)$/
Copy the code

You can copy the old iron directly

Regular expression in JS

Keep a good collection

// Verify that the input is null
			isNull:function(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = /\S/;
				if(! regex.test(str)){ alert("Text box cannot be empty, please enter content!"); }}Copy the code
// Verify that the entered characters are English letters
			isLetter:function(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = /^[a-zA-Z]+$/;
				if(! regex.test(str)){ alert("Please enter the correct English letter!"); }}Copy the code
// Verify that the date format is YYYY-MM-DD
isDate:function(idStr){
			
				var str = document.getElementById(idStr).value.trim();
				var regex = / ^ (\ d {1, 4}) (-) | \ / (\ d {1, 2}) \ 2 $/ (\ d {1, 2});
				var r = str.match(regex); // Get the value of the specified string using the match method

				if(r==null){
					alert("Please enter the correct date format!"); }}Copy the code
	// Check whether the date format is YYYY-MM-DD hh: MM :ss
isDateTime:function(idStr){
			
				var str = document.getElementById(idStr).value.trim();
				var regex = / ^ (\ d {1, 4}) (-) | \ / (\ d {1, 2}) \ 2 (\ d {1, 2}) (\ d {1, 2}) : (\ d {1, 2}) : (\ d {1, 2}) $/;
				var r = str.match(regex); // Get the value of the specified string using the match method

				if(r==null){
					alert("Please enter the correct date format!"); }}Copy the code
// Validate the integer
isInteger:function(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = / ^ +] [-? \d*$/;
				if(! regex.test(str)){ alert("Please enter a correct integer!"); }}Copy the code
// Verify double precision
isDouble:function(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = / ^ [\ +]? \d+(\.\d+)? $/;
				if(! regex.test(str)){ alert("Please enter the correct decimal!"); }}Copy the code
// Verify Chinese
isChinese(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = /^[\u0391-\uFFE5]+$/;
				if(! regex.test(str)){ alert("Please enter the correct Chinese!"); }}Copy the code
// Verify the mailbox
isEmail(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
				if(! regex.test(str)){ alert("Please enter the correct email format!"); }}Copy the code
// Verify the phone number
isPhone(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = /^1[3456789]\d{9}$/;
				if(! regex.test(str)){ alert("Please enter the correct phone number!"); }}Copy the code
// Verify id
isIdCard(idStr){
				var str = document.getElementById(idStr).value.trim();
				var regex = /^\d{6}(18|19|20)? \d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;
				if(! regex.test(str)){ alert("Please input the correct ID number!"); }}Copy the code