RegExp
- Direct quantity (recommended)
- new RegExp();
- I (ignoreCase), g, m
- \w === [0-9A-z_]
- \W === [^\w]
- \d === [0-9]
- \D === [^\d]
- \s === [\t\n\r\v\f]
- \S === [^\s]
- Unicode code
var reg = /\u4f60\u597d/g;
var str = 'hello';
console.log(str.match(reg));
Copy the code
quantifiers
// n+ {1, } // n* {0, } // n? {0, 1} / / n {X} {X} / / n {X, y} {X, y} / / n {X} {X} / / by ^ $limit begin ending / / var reg = / \ w * / g; // var str ="abc"; // console.log(str.match(reg)); / / /'abc'.' '] Logically a match was made first"abc"// var reg = /\d*/g; // console.log(str.match(reg)); / / /' '.' '.' '.' '] / / matching principle: greed matching principle / / https://www.w3school.com.cn/jsref/jsref_obj_regexp.asp / / var/reg = ^ ABC $/ g; // var str ="abc";
// console.log(str.match(reg));
Copy the code
Tests whether a string begins or ends with a number
Var reg = /^\d[\s]*\d$/g; var reg = /^\d[\s]*\d$/g; / / fore and aft contain digital var reg2 = / ^ \ | d \ d $/ g; // Start or end with a number var STR ="123abc1";
console.log(reg.test(str)); // true
Copy the code
Regular properties
- LastIndex global ignoreCase multiline source
exec()
Var reg = /ab/g; var reg = /ab/g; var str ='abababab'; console.log(reg.lastIndex); // 0 console.log(reg.exec(str)); / / /'ab', index: 0, input: 'abababab'] console.log(reg.lastIndex); // 2 console.log(reg.exec(str)); / / /'ab', index: 2, input: 'abababab'] console.log(reg.lastIndex); // 4 console.log(reg.exec(str)); / / /'ab', index: 4, input: 'abababab'] console.log(reg.lastIndex); // 6 console.log(reg.exec(str)); / / /'ab', index: 6, input: 'abababab'] console.log(reg.lastIndex); // 8 console.log(reg.exec(str)); // null console.log(reg.lastIndex); // 0 // Re does not add g, lastIndex always starts from zeroCopy the code
Matches four identical values
var str = "aaaa"; var reg = /(\w)\1\1\1/g; // \1 Backreference the content referenced by the first subexpression console.log(reg.test(STR));Copy the code
matchingaabb
The value of the
var str = 'aabb'; var reg = /(\w)\1(\w)\2/g; // \1 Backreference the content referenced by the first subexpression console.log(reg.test(STR));Copy the code
String object method
// The re does not add g, match returns similarexec(), add g,match returns array // search returns directory // split // replace most commonly usedCopy the code
replace
var str = "aaaa";
console.log(str.replace("a"."b")); Var reg = /a/g; console.log(str.replace(reg,'b')); // bbbb */
var reg = /(\w)\1(\w)\2/g;
var str = "aabb";
console.log(str.replace(reg, "$2$2The $1The $1"));
var reg = /(\w)\1(\w)\2/g;
var str = "aabb";
console.log(str.replace(reg, function ($, The $1.$2) {
return $2 + $2 + The $1 + The $1 + "abc"; // "bbaaabc"
}));
Copy the code
the-first-name -> theFirstName
var reg = /-(\w)/g;
var str = "the-first-name";
console.log(str.replace(reg, function ($, The $1) {
return The $1.toUpperCase();
})); // theFirstName
Copy the code
Forward lookup forward assertion
var str = "abaaaa"; var reg = /a(? =b)/g; // a? = A =b = a =b a? ! B, a not followed by b console.log(str.match(reg)); / / /'a' ]
Copy the code
Non-greedy match,? Breaking the greedy match
var reg = /a+? /g; // can take 1 but not 1+Copy the code
Regular to heavy
var str = "aaaaaaabbbbbbbcccccc";
var reg = /(\w)\1*/g;
console.log(str.replace(reg, "The $1")); // abc
Copy the code