Home>

Speaking of regular expressions,There are many universal expressions online,But in fact,Most people are unwilling to study it.Just use it and just use it.But, in fact, sometimes,The actual situation in the project or company is different,You need to modify the regular expression.Customized according to the actual situation,This requires us to seriously learn some ways of writing regular expressions.All-takenism,While letting us enjoy rich network sharing resources,Also brought us inertia,Regarding some use cases of regular expressions,I won't make more introductions,There are many introductions online.. (/go.php?id=72346&s=a(Some basic uses of regular expressions)

Some time ago,We will verify the email in the project.Allow multiple emails to be separated by;and then, when submitting in the page form,Perform corrective actions,A single message or multiple messages,is it right or not.About multiple emails,I ’m not going to say a single email, when multiple,Just use split (";") to generate an array,Then loop through each email to determine if it is the correct email format.This is not the point we are talking about,I just want to explain here,I need to verify the situation here..

Friends, if you have any questions about regular expressions,Welcome to leave a message to discuss!

The original regular expression

/^[a-za-z0-9_-][email protected][a-za-z0-9_-]+(\.[a-za-z0-9_-]+)+$/;

I modified the regular expression according to my actual situation

/^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/;

or

/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;

Character description:

^:Match the starting position of the input.

\:Mark the next character as a special character or literal.

*:Match the previous character zero or several times.

+:Match the previous character one or more times.

(pattern) Matches a pattern and remembers to match.

x | y:matches x or y.

[a-z]:Represents characters in a range.Matches any character within the specified interval.

\ w:matches any word character,Include underscores.

(n, m) matches at least n times and at most m times

$:Matches the end of the input.

Attached with a simple js

function checkemail ()
 {
 var emailvalue=document. getelementbyid_r ("email"). value;
 if (! isemail (emailvalue))
 {
 alert ("The email you entered is incorrect,Please recheck and enter again! ");
 document. getelementbyid_r ("email"). focus ();
 return false;
 }
 return true;
 }
 function isemail (str) {
  var reg=/^(\w)+(\.\w+)*@(\w)+((\.\w+)+)$/;
  return reg.test (str);
 }

//Verify that the mailbox format is correct 20080602_heyitang

var email=document. getelementbyid_r ("trans_email"). value;

//If, the user only needs to make a judgment after entering the mailbox

if (email!=null)
 {if (email.indexof (";") ==-1)
 {
 if (! isemail (email))
 {
  alert ("The single message you entered is malformed.
Please check again before entering ");
  document. getelementbyid_r ("trans_email"). focus ();
  return false;
 }
 }
 else
 {
 var emailarray=email.split (";");
 for (i=0;i<emailarray.length;i ++)
 {
  //[email protected] is prevented here;[email protected];added more;at this time, there may be elements in the array without content
  if (emailarray [i]!=null || emailarray [i]!="")
  {
  if (! isemail (emailarray [i]))
  {
  alert ("You have entered multiple mailbox formats with incorrect mailbox formats, please recheck and enter again");
  document. getelementbyid_r ("trans_email"). focus ();
  return false;
  }
  }
 }
 }
 }
  • Previous JQuery menu effect two examples explain (3)
  • Next Android context menu usage example analysis