Preface
Example: ^.+@.+\\..+$
This code has repeatedly put myself to scare. Many people may also be such code to scare away. Continue reading this article will let you can also free application code like this.
The text
Tutorial: regular expression tutorial 30 minutes
A study of regular expressions good tutorial, interested in regular expressions, but do not know much about the shoes can refer to the next
The following is an excerpt from the JQuery digital type validation regex finishing and classic Javascript regular expressions
[ A backup:) ~~ ]
The commonly used JQuery digital type validation regex finishing (there are many wrong place, remember the appropriate modifications)
var regexEnum = {
intege:/^-?[1-9]*$/, //Integer
intege1:/^[1-9]*$/, //A positive integer
intege2:/^-[1-9]*$/, //Negtive integer
num:/^([+-]?)\d*\.?\d+$/, //Digital
num1:/^([1-9]*|0)$/, //Positive (positive integer + 0)
num2:/^-[1-9]*|0$/, //Negative (negative integer + 0)
decmal:/^([+-]?)\d*\.\d+$/, //Floating point number
decmal1:/^[1-9]*.\d*|0.\d*[1-9]\d*$/, //Positive floating-point number
decmal2:/^-([1-9]*.\d*|0.\d*[1-9]*)$/, //Negative float
decmal3:/^-?([1-9]*.\d*|0.\d*[1-9]*|0?.0+|0)$/, //Floating point number
decmal4:/^[1-9]*.\d*|0.\d*[1-9]*|0?.0+|0$/, //Non negative float (positive floating-point number + 0)
decmal5:/^(-([1-9]*.\d*|0.\d*[1-9]*))|0?.0+|0$/, //Non positive float (negative float + 0)
email:/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/, //Mail
color:/^[a-fA-F0-9]{6}$/, //Color
url:/^http[s]?:\/\/([\w-]+\.)+[\w-]+([\w-./?%&=]*)?$/, //url
chinese:/^[\u4E00-\u9FA5\uF900-\uFA2D]+$/, //Chinese only
ascii:/^[\x00-\xFF]+$/, //Only ACSII characters
zipcode:/^\d{6}$/, //Zip code
mobile:/^(13|15|18)[0-9]{9}$/, //Mobile phone
notempty:/^\S+$/, //Non empty
picture:/(.*)\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$/, //The picture
rar:/(.*)\.(rar|zip|7zip|tgz)$/, //The compressed file
date:/^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/, //Date
qq:/^[1-9]*[1-9][0-9]*$/, //QQ number
tel:/^(([0+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/, //Function of telephone number (including the area code verification, Intl., extension)
username:/^\w+$/, //For user registration. Matching by the numbers, strings of 26 English letters or underline.
letter:/^[A-Za-z]+$/, //Letter
letter_u:/^[A-Z]+$/, //Capital
letter_l:/^[a-z]+$/, //Lowercase letters
idcard:/^[1-9]([0-9]{14}|[0-9]{17})$/ //ID
}
The classical Javascript regular expressions ( there are many wrong place, remember that the revised )
Regular expression matching Chinese characters:
[\u4e00-\u9fa5]
Matching double byte characters (including Chinese characters included):
[^\x00-\xff]
Application: to calculate the length of the string (a double byte character length of 2 characters, ASCII 1)
String.prototype.len=function(){ return this.replace([^\x00-\xff]/g,"aa").length; }
Regular expression matching dakini:
\n[\s|]*\r
Regular expression matching HTML markers:
/<(.*)>.*<\/\1>|<(.*) \/>/
Regular expression matching both spaces:
(^\s*)|(\s*$)
Application: no VBScript like trim JavaScript function, we can use this expression to achieve, as follows:
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
Decomposition and conversion of IP to address the use of regular expressions
Below is a match the IP address by using regular expressions, and the IP address into the corresponding numerical program Javascript:
function IP2V(ip)
{
re=/(\d+)\.(\d+)\.(\d+)\.(\d+)/g //Regular expression matching IP address
if(re.test(ip))
{
return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1
}
else
{
throw new Error("Not a valid IP address!")
}
}
However, the above procedure without regular expressions, and the direct use of split function to decomposition may be more simple, the procedure is as follows:
var ip="10.100.20.168"
ip=ip.split(".")
alert("The IP value is: "+(ip[0]*255*255*255+ip[1]*255*255+ip[2]*255+ip[3]*1))
Regular expression matching Email address:
\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*
Regular expression matching. URL:
http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
Remove duplicate character string using the regular expression algorithm procedures: [* Note: this procedure is not correct.
var s="abacabefgeeii"
var s1=s.replace(/(.).*\1/g,"$1")
var re=new RegExp("["+s1+"]","g")
var s2=s.replace(re,"")
alert(s1+s2) //Results for the: abcefgi
*Note
===============================
If the VaR s = "abacabefggeeii"
The result is wrong, the results for the: abeicfgg
The ability of regular expressions Co.
===============================
I originally posted on CSDN for an expression method to realize the removal of duplicate characters, it is not found there, it is a simple method to realize I can think of. Idea is to use the back reference to remove including repeated characters, then repeated characters to establish second expressions, to not repeat the characters, both series. This method has the string may not apply for the sequence of characters.
To use regular expressions to extract the file name from the URL address of the JavaScript program, the following result is page1
s="http://blog.penner.cn/page1.htm"
s=s.replace(/(.*\/){ 0, }([^\.]+).*/ig,"$2")
alert(s)
Restrictions on the text box input webpage form by using regular expressions:
Limited to input Chinese with regular expressions:
onkeyup="value=value.replace(/[^\u4E00-\u9FA5]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))"
Limited input full width characters with regular expressions:
onkeyup="value=value.replace(/[^\uFF00-\uFFFF]/g,'')" onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))"
Restrictions can only enter numbers using regular expressions:
onkeyup="value=value.replace(/[^\d]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
Restrictions can only enter numbers and English with regular expressions:
onkeyup="value=value.replace(/[\W]/g,'') "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))"
Matching non negative integer (integer + 0)
^\d+$
Matching positive integer
^[0-9]*[1-9][0-9]*$
Matching non positive integer (negative integer + 0)
^((-\d+)|(0+))$
Matching negative integer
^-[0-9]*[1-9][0-9]*$
Matching integer
^-?\d+$
Matching non negative float (positive floating-point number + 0)
^\d+(\.\d+)?$
Matching is floating point
^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
Matching is non floating point (negative float + 0)
^((-\d+(\.\d+)?)|(0+(\.0+)?))$
Matching negative float
^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
Matching float
^(-?\d+)(\.\d+)?$
A matching string composed of 26 English letters
^[A-Za-z]+$
A matching string composed of 26 English letters.
^[A-Z]+$
A matching string composed of 26 English letters.
^[a-z]+$
A matching string composed of digital and 26 English letters
^[A-Za-z0-9]+$
Matching by the numbers, strings of 26 English letters or underline.
^\w+$
Match email address
^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$
Matching URL
^[a-zA-z]+://Matching(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
Matching HTML tag
<\s*(\S+)(\s[^>]*)?>(.*?)<\s*\/\1\s*>