regular expression:RegExp
Regular expressions can be used test () for verification, can also be used match (), exec () as capture
let str = "good good study , day day up!"; REG the let = / \ D + /; // check whether digital reg.test (STR); // => to false STR = "2019-10-01" ; reg.exec (STR); // => [ "2019", index: 0, Inputs: "original string"]
1. Writing Regular Expressions
There are two ways to create:
// => Create embodiment literals (wrap between two slashes are used to describe the rule meta character) the let REG1 = / \ D + / ; // => Mode constructor creates two parameters: meta-character string, the string qualifier the let REG2 = new new the RegExp ( "\\ + D");
Regular expression consists of two parts, meta characters and modifiers
Yuan characters:
Quantifier metacharacters: Set the number of occurrences
* // zero to several + // a plurality of times to ? // zero or a {n} // occurs n times {n,} // occurs n times to {n, m} // appear to n m times
Special Character Element: single or together in combination represent special meaning
\ // escape character (Normal -> Special -> General) . // any character except \ than n (newline) of ^ // to which metacharacters begin with $ // to which meta-characters as the end \ n- // newline \ D // a number between 0 to 9 \ D // a number between 0 to 9, a non-(uppercase and lowercase are opposite in meaning) \ W // numbers, letters, underline any one character \ S // a blank characters (including spaces, tabs, formfeeds, etc.) \ T // a tab character (a TAB key: four spaces) \ B // matches a word boundary x | y // a character in the x or y [XYZ] // x or y or z is a character of [^ XY] // any character except of x / y [az] // specify any characters az this range [0-9a-zA-the Z _] === \ W [^ az] // one of negation "non" () // regular grouping symbol is in the (? :) // only match not capture (? =) // positive pre-investigation (?!) // negative to pre-check
Common metacharacters: meaning behalf of itself
/ the Hello / // this regular match is "hello"
Second, the modifier:
/ * Regular expression commonly used modifiers: img * / I => the ignoreCase // Ignore words match the case m => the multiline // may be multi-line matching G => Global // Global Match /A/.test('lalala') =>false /A/i.test('lalala') =>true
Detailed application metacharacters
^ $
let reg = /^\d/; console.log(reg.test("hello")); //=>false console.log(reg.test("2019hello"));//=>true console.log(reg.test("hello2019"));//=>false let reg = /\d$/; console.log(reg.test("hello")); //=>false console.log(reg.test("2019hello"));//=>false console.log(reg.test("hello2019"));//=>true // => ^ / $ plus two do not: the string contains content to conform to the rules of the let reg1 = / \ d + / ; // => ^ / $ plus two are: strings and rules can only be consistent content the let REG2 = / ^ \ + $ D / ; // => For example: validation phone number (11 bits, the first number is 1 to) the let REG = / ^ 1 \ {D} $ 10 /;
\
let reg = /^2.3$/; console.log(reg.test("2.3")); //=>true console.log(reg.test("2@3")); //=>true console.log(reg.test("23")); //=>false // => Based on the escape character, allowed only represent the decimal REG = /^2\.3$/ ; console.log(reg.test("2.3")); //=>true console.log(reg.test("2@3")); //=>false let str = "\\d"; REG = / ^ \ D $ /; // => \ D 0-9 representative of the console.log (reg.test (STR)); // => to false REG = / ^ $ D \\ /; // => converted to meet the special Common the console.log (reg.test (STR)); // => to true
x | and
let reg = /^18|29$/; the console.log (reg.test ( "18 is")); // => to true the console.log (reg.test ( "29")); // => to true the console.log (reg.test ( "129") ); // => to true the console.log (reg.test ( "189")); // => to true the console.log (reg.test ( "1829")); // => to true the console.log (REG .test ( "829")); // => to true console.log (reg.test ( "182")); // => to true // --- direct x | y there will be a mess priority issues generally we write when they are accompanied by a small grouping parentheses, because priority => parentheses parentheses change process: a packet REG = / ^ (18 | 29) $ / ; the console.log (reg.test ( "18 is")); // => to true the console.log (reg.test ( "29")); // => to true the console.log (reg.test ( "129") ); // => to false the console.log (reg.test ( "189")); // => to false // => can only be a 18 or a 29
// characters appear in brackets 1. General represents the meaning itself of the let REG = / ^ [@ +] $ / ; console.log(reg.test("@")); //=>true console.log(reg.test("+")); //=>true console.log(reg.test("@@")); //=>false console.log(reg.test("@+")); //=>false reg = /^[\d]$/; //=>\d在中括号中还是0-9 console.log(reg.test("d"));//=>false console.log(reg.test("\\"));//=>false console.log(reg.test("9"));//=>true // do not exist in the brackets 2. multibit REG = / ^ [18 is] $ / ; console.log(reg.test("1")); //=>true console.log(reg.test("8")); //=>true console.log(reg.test("18")); //=>false reg = /^[10-29]$/; //=>1或者0-2或者9 console.log(reg.test("1"));//=>true console.log(reg.test("9"));//=>true console.log(reg.test("0"));//=>true console.log(reg.test("2"));//=>true console.log(reg.test("10"));//=>false
A few examples:
1. Verify that a valid number
/ * Rule Analysis * 1. may arise + - number, or may not occur [+ -]? * 2 can be a 0-9, first place can not be more than 0 (\ d | ([1-9] \ d +)) * 3. fractional part probably is not possible, if there must be followed by a decimal point numbers + (\. \ D +)? */ let reg = /^[+-]?(\d|([1-9]\d+))(\.\d+)?$/;
2. Verify Password
// => numbers, letters, underline @ => 6 ~ 16 the let Val = userPassInp.value, reg = /^\w{6,16}$/; let flag=reg.test(val); function checkPass(val){ if(val.length<6 || val.length>16){ Alert ( 'must be between 6-16!' ); return ; } Area the let = [ 'A', 'B' ....'_ ']; // => contain numbers, letters, underline for (the let I = 0; I <val.length; I ++ ) { let char=val[i]; if(!area.includes(char)){ Alert ( 'format is not correct!' ); return ; } } }
3. Verify real name
/* * 1. Chinese characters / ^ [\ u4E00- \ u9FA5] $ / 2. The length of the name ~ 2 * 10 * 3. Some translation Kanji (· [\ u4E00- \ u9FA5] {2,10}) {0,2} */ let reg = /^[\u4E00-\u9FA5]{2,10}(·[\u4E00-\u9FA5]{2,10}){0,2}$/;
4. Verify mailbox
let reg = /^\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; // => \ W + ((- \ + W) |. (\ \ W +)) * // 1. begin underscore alphanumeric character (to more than 1) // 2 may also be - alphanumeric numbers or underscores. letters underscore the overall zero to many times
5. ID number (matching specific code number but also with provincial cities and counties throughout)
/* 1. A total of 18 * * 2. The last one may be the X * * ID card before six: provincial cities and counties * Middle eight: date * The last four: * Last => X or digital * Penultimate => even-odd men and women * The rest is calculated through algorithm * / // the let REG = / ^ \ {D}. 17 (\ D | X-) $ /; // => The second effect parentheses packet: packet capture, not only the information matching the Taisho captured, can also capture separately to the content of each of small packets let reg = / ^ (\ d {6}) (\ d {4}) (\ d {2}) (\ d {2}) \ d {2} ( \ d) (\ d | the X-) $ / ; reg.exec ( "123456789012345678"); // => captured result is an array, each small packet acquisition comprising separate content