- Thousands separator notation for numbers 123333 -> 123,333
const reg = /\B(? =(\d{3})+\b)/g;
Copy the code
- The password contains 6 to 12 characters including digits, lowercase characters, and uppercase letters, but must contain digits
const reg = / (? =.*[0-9])^[a-zA-Z0-9]{6, 12}$/;
Copy the code
- The password contains 6 to 12 characters, including digits, lowercase letters, and uppercase letters
const reg = / (? =. * ([0-9])? =.*[a-z])^[a-zA-Z0-9]{6, 12}$/;
Copy the code
- The password contains 6 to 12 characters, including digits, lowercase characters, and uppercase letters, but must contain at least two types of characters
const reg = / ((? =. * ([0-9])? =.*[a-z])|(? =. * ([0-9])? =.*[A-Z])|(? =.*[a-z])(? =. * [a-z])) ^ [0-9 A Za - Z] {6, 12} $/;
Copy the code
const reg = / (? ! ^ [0-9] {12} $) (? ! ^ [a-z] {12} $) (? ! ^ [a-z] {12} $) ^ [0-9 A Za - Z] {6, 12} $/;
Copy the code
- Yyyy-mm-dd format, replaced with mm/ DD/YYYY,1, 1,1, 2, and $3 refer to the 1/2/3 matched grouping, respectively
const rege = /(\d{4})-(\d{2})-(\d{2})/;
const str1 = "2017-06-12";
const res1 = str1.replace(rege, "$2 / $3 / $1");
// This is equivalent to:
const res2 = str1.replace(rege, function () {
return RegExpThe $2 +"/" + RegExp. $3 +"/" + RegExp. $1; });const res3 = str1.replace(rege, function (match, year, month, day) {
return month + "/" + day + "/" + year;
});
Copy the code
- The backreference \1 represents the first group that was previously matched, \2 \3 represents the second and third groups
const reg = /\d{4}(-|\/|\.) \d{2}\1\d{2}/;
Copy the code
- Preview (? =exp) /? <=exp)/negative outlook (? ! Exp)/negative afterthought (?
() represents the capture group, and () saves the matched value of each group, using $n(n is a number representing the contents of the NTH capture group).
(? 🙂 represents a non-capture group. The only difference is that values matched by a non-capture group are not saved
- The trim method
const reg = /^\s+|\s+$/g;
const str2 = ' footer ';
str2.replace(reg, ' ') // equivalent to str2.trim()
Copy the code
- Capitalize the first letter of each word
const regex9 = / (? :^|\s)\w/g;
function titleize(str) {
return str.toLowerCase().replace(regex9, function(c) {
returnc.toUpperCase(); })}Copy the code
- Hump,
const regex10 = /[-_\s]+(.) ? /g;
function camelize (str) {
return str.replace(regex10, function (match, c) {
return c ? c.toUpperCase() : ' ';
});
}
Copy the code
- Neutralization (inverse hump process)
function dasherize (str) {
return str.replace(/([A-Z])/g.'- $1').replace(/[-_\s]+/g.The '-').toLowerCase();
}
Copy the code