Home > Article > Backend Development > Regular expression to match email address in php_PHP tutorial
An example of a regular expression for matching email addresses in PHP. The regular matching expression I commonly use when replacing email addresses: /^[a-z]([a-z0-9]*[-_]?[a-z0 -9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z ]{2})?$/i, the following is a detailed analysis for friends in need.
php example
The following uses PHP as an example:
代码如下 | 复制代码 |
< ?php if (ereg(“/^[a-z]([a-z0-9]*[-_.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[.][a-z]{2,3}([.][a-z]{2})?$/i; ”,$email)) { echo “Your email address is correct!”; } else { echo “Please try again!”; } ?> |
Description:
①/content/i forms a case-insensitive regular expression; ^ starts the match; $ ends the match.
②[a-z] E-Mail prefix must start with an English letter
③([a-z0-9]*[-_]?[a-z0-9]+)* matches _a_2, aaa11, _1_a_2, but does not match a1_, aaff_33a_, a__aa, if it is a null character , is also matched, * means 0 or more.
④* represents 0 or more preceding characters.
⑤[a-z0-9]* matches 0 or more English letters or numbers; [-_]? matches 0 or 1 "-", because "-" cannot appear continuously.
⑥[a-z0-9]+ matches 1 or more English letters or numbers, because "-" cannot be used as the end
⑦@ There must be someone @
⑧([a-z0-9]*[-_]?[a-z0-9]+)+ See above ([a-z0-9]*[-_]?[a-z0-9 ]+)* explanation, but it cannot be empty, + means one or more.
⑨[.] treats special characters (.) as ordinary characters; [a-z]{2,3} matches 2 to 3 English letters, usually com or net, etc.
⑩([.][a-z]{2})? Matches 0 or 1 [.][a-z]{2} (such as .cn, etc.) I don’t know if the last part of .com.cn is usually two bit, if not, please change {2} to {number of starting words, number of ending words}
js example
The code is as follows
|
Copy code
|
||||
<script><br> function Email(ee){<br> var emailreg = "^w+@w+.w+(.w+)*$";<br> <br>var rege = new RegExp(emailreg, 'g');<br> alert(rege.test(ee)); }</td> var ee1 = "12xwz@123e^rsrf6.csdfdfom.df";</tr> var ee2 = "12xwz@123ersrf6.csdfdfom.df";</table> Email(ee1); Email(ee2);<p align="left"> </script> Statement: The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn Previous article:PHP recursive traversal traverses all files and sub-files in a folder_PHP tutorialNext article:PHP recursive traversal traverses all files and sub-files in a folder_PHP tutorial Related articlesSee more |