A Simple Verification Method for js Regular Expressions

  • 2021-10-16 00:47:01
  • OfStack

Some operations on strings can be realized by regular expressions. 1-like search operation must have been learned by everyone. Today, let's talk about its verification function, which can help judge the string type or other components, such as password, Chinese, string composition and so on. Below, the validation of js regular expressions brings content sharing, taking into account the types supported in js.

1. Common js regular check

(1) Verify password strength

The strength of the password must be a combination of upper and lower case letters and numbers, no special characters can be used, and the length is between 8 and 10.


^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$

(2) Verify Chinese

String can only be in Chinese.


^[\\u4e00-\\u9fa5]{0,}$

(3) A string consisting of numbers, 26 English letters or underscores


^\\w+$

2. js supported and unsupported types

js support

Most metacharacters Character group Paragraph start and end, and pseudo-logical lines Matching priority quantifiers. * and ignoring priority quantifiers. *? Looking forward? = Reverse reference\ 1\ 2 Non-captured packets? : Common modifier/igx What js does not support are Reverse look around (reverse assertion)? < = Named grouping? = p Grouping internal modifier (? = i) Curing grouping? > Place-occupying quantifier. * +

js Regular Expression Validation Example


/* Is there a decimal number */
function  isDecimal(strValue ) { 
  var objRegExp= /^\d+\.\d+$/;
  return objRegExp.test(strValue); 
} 

/* Verify whether it is composed of Chinese names  */
function ischina(str) {
  var reg=/^[\u4E00-\u9FA5]{2,4}$/;  /* Define validation expressions */
  return reg.test(str);   /* Validate */
}

/* Verify whether it is all by 8 Bit number composition  */
function isStudentNo(str) {
  var reg=/^[0-9]{8}$/;  /* Define validation expressions */
  return reg.test(str);   /* Validate */
}

/* Verify the telephone code format  */
function isTelCode(str) {
  var reg= /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/;
  return reg.test(str);
}

/* Verify that the email address is legal  */
function IsEmail(str) {
  var reg=/^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
  return reg.test(str);
}

Related articles: