摘要:收集一些經常使用的正則表達式。javascript
正則表達式用於字符串處理,表單驗證等場合,實用高效,但用到時老是不太把握,以至每每要上網查一番。我將一些經常使用的表達式收藏在這裏,做備忘之用。本貼隨時會更新。
匹配中文字符的正則表達式:html
匹配雙字節字符(包括漢字在內):java
應用:計算字符串的長度(一個雙字節字符長度計2,ASCII字符計1)正則表達式
String
.
prototype
.
len
=
function
(){
return
this
.
replace
([
^\
x00
-\
xff
]
/
g,"aa").length; }
匹配空行的正則表達式:算法
匹配HTML標記的正則表達式:數據庫
/
<(.*)>.*<
\/
\1>|<(.*)
\/
>
/
匹配首尾空格的正則表達式:函數
應用:j avascript中沒有像v bscript那樣的trim函數,咱們就能夠利用這個表達式來實現,以下:this
String
.
prototype
.
trim
=
function
()
{
return
this
.
replace
(
/
(^\s*)|(\s*$)
/g
,
""
)
;
}
利用正則表達式分解和轉換IP地址
下面是利用正則表達式匹配IP地址,並將IP地址轉換成對應數值的Javascript程序:url
function
IP2V
(
ip
)
{
re
=
/
(\d+)\.(\d+)\.(\d+)\.(\d+)
/g
//匹配IP地址的正則表達式
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!
"
)
}
}
不過上面的程序若是不用正則表達式,而直接用split函數來分解可能更簡單,程序以下:spa
var
ip
=
"
10.100.20.168
"
ip
=
ip
.
split
(
"
.
"
)
alert
(
"
IP值是:
"
+
(
ip
[
0
]
*
255
*
255
*
255
+
ip
[
1
]
*
255
*
255
+
ip
[
2
]
*
255
+
ip
[
3
]
*
1
))
匹配Email地址的正則表達式:
\
w
+
([
-+.
]
\
w
+
)
*@\
w
+
([
-.
]
\
w
+
)
*\.\
w
+
([
-.
]
\
w
+
)
*
匹配網址URL的正則表達式:
http
:
//([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
利用正則表達式去除字串中重複的字符的算法程序:[*注:此程序不正確]
var
s
=
"
abacabefgeeii
"
var
s1
=
s
.
replace
(
/
(.).*\1
/g
,
"
$1
"
)
var
re
=
new
RegExp
(
"
[
"
+
s1
+
"
]
"
,
"
g
"
)
var
s2
=
s
.
replace
(
re
,
""
)
alert
(
s1
+
s2
)
//結果爲:abcefgi
*注
===============================
若是var s = 「abacabefggeeii」
結果就不對了,結果爲:abeicfgg
正則表達式的能力有限
===============================
我原來在CSDN上發貼尋求一個表達式來實現去除重複字符的方法,最終沒有找到,這是我能想到的最簡單的實現方法。思路是使用後向引用取出包括重複的字符,再以重複的字符創建第二個表達式,取到不重複的字符,二者串連。這個方法對於字符順序有要求的字符串可能不適用。
得用正則表達式從URL地址中提取文件名的javascript程序,以下結果爲page1
s
=
"
http://blog.penner.cn/page1.htm
"
s
=
s
.
replace
(
/
(.*
\/
){ 0, }([^\.]+).*
/i
g
,
"
$2
"
)
alert
(
s
)
利用正則表達式限制網頁表單裏的文本框輸入內容:
用正則表達式限制只能輸入中文:
onkeyup
=
"
value=value.replace(/[^\u4E00-\u9FA5]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))
"
用正則表達式限制只能輸入全角字符:
onkeyup
=
"
value=value.replace(/[^\uFF00-\uFFFF]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))
"
用正則表達式限制只能輸入數字:
onkeyup
=
"
value=value.replace(/[^\d]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))
"
用正則表達式限制只能輸入數字和英文:
onkeyup
=
"
value=value.replace(/[\W]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))
"
匹配非負整數(正整數 + 0)
匹配正整數
^
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*$
匹配非正整數(負整數 + 0)
^
((
-\
d
+
)
|
(
0
+
))
$
匹配負整數
^-
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*$
匹配整數
匹配非負浮點數(正浮點數 + 0)
匹配正浮點數
^
(([
0
-
9
]
+\.
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*\.
[
0
-
9
]
+
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
))
$
匹配非正浮點數(負浮點數 + 0)
^
((
-\
d
+
(
\.\
d
+
)
?
)
|
(
0
+
(
\
.0
+
)
?
))
$
匹配負浮點數
^
(
-
(([
0
-
9
]
+\.
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*\.
[
0
-
9
]
+
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)))
$
匹配浮點數
^
(
-?\
d
+
)(
\.\
d
+
)
?$
匹配由26個英文字母組成的字符串
匹配由26個英文字母的大寫組成的字符串
匹配由26個英文字母的小寫組成的字符串
匹配由數字和26個英文字母組成的字符串
匹配由數字、26個英文字母或者下劃線組成的字符串
匹配email地址
^
[
\
w
-
]
+
(
\.
[
\
w
-
]
+
)
*@
[
\
w
-
]
+
(
\.
[
\
w
-
]
+
)
+$
匹配url
^
[
a
-
zA
-
z
]
+:
//匹配(\w+(-\w+)*)(\.(\w+(-\w+)*))*(\?\S*)?$
匹配html tag
<\
s
*
(
\
S
+
)(
\
s
[
^>
]
*
)
?>
(
.*?
)
<\
s
*\
/
\1\s*>
Visual Basic & C# Regular Expression
1.確認有效電子郵件格式
下面的示例使用靜態 Regex.IsMatch 方法驗證一個字符串是否爲有效電子郵件格式。若是字符串包含一個有效的電子郵件地址,則 IsValidEmail 方法返回 true,不然返回 false,但不採起其餘任何操做。您可使用 IsValidEmail,在應用程序將地址存儲在數據庫中或顯示在 ASP.NET 頁中以前,篩選出包含無效字符的電子郵件地址。
[Visual Basic]
Function IsValidEmail(strIn As String) As Boolean
' Return true if strIn is in valid e-mail format.
Return Regex.IsMatch(strIn, ("^([\w-\.]+)@((\[[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.)|(([\w-]+\.)+))([a-zA-Z]{ 2,4 }|[0-9]{ 1,3 })(\]?)$")
End Function
[C#]
bool IsValidEmail(string strIn)
{
// Return true if strIn is in valid e-mail format.
return Regex.IsMatch(strIn, @"^([\w-\.]+)@((\[[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.[0-9]{ 1,3 }\.)|(([\w-]+\.)+))([a-zA-Z]{ 2,4 }|[0-9]{ 1,3 })(\]?)$");
}
2.清理輸入字符串
下面的代碼示例使用靜態 Regex.Replace 方法從字符串中抽出無效字符。您可使用這裏定義的 CleanInput 方法,清除掉在接受用戶輸入的窗體的文本字段中輸入的可能有害的字符。CleanInput 在清除掉除 @、-(連字符)和 .(句點)之外的全部非字母數字字符後返回一個字符串。
[Visual Basic]
Function CleanInput(strIn As String) As String
' Replace invalid characters with empty strings.
Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function
[C#]
String CleanInput(string strIn)
{
// Replace invalid characters with empty strings.
return Regex.Replace(strIn, @"[^\w\.@-]", "");
}
3.更改日期格式
如下代碼示例使用 Regex.Replace 方法來用 dd-mm-yy 的日期形式代替 mm/dd/yy 的日期形式。
[Visual Basic]
Function MDYToDMY(input As String) As String
Return Regex.Replace(input, _
"\b(?
\d{ 1,2 })/(?
\d{ 1,2 })/(?
\d{ 2,4 })\b", _
"${ day }-${ month }-${ year }")
End Function
[C#]
String MDYToDMY(String input)
{
return Regex.Replace(input,"\\b(?
\\d{ 1,2 })/(?
\\d{ 1,2 })/(?
\\d{ 2,4 })\\b","${ day }-${ month }-${ year }");
}
Regex 替換模式
本示例說明如何在 Regex.Replace 的替換模式中使用命名的反向引用。其中,替換表達式 ${ day } 插入由 (?
…) 組捕獲的子字符串。
有幾種靜態函數使您能夠在使用正則表達式操做時無需建立顯式正則表達式對象,而 Regex.Replace 函數正是其中之一。若是您不想保留編譯的正則表達式,這將給您帶來方便
4.提取 URL 信息
如下代碼示例使用 Match.Result 來從 URL 提取協議和端口號。例如,「http://www.penner.cn:8080……將返回「http:8080」。
[Visual Basic]
Function Extension(url As String) As String
Dim r As New Regex("^(?
\w+)://[^/]+?(?
:\d+)?/", _
RegexOptions.Compiled)
Return r.Match(url).Result("${ proto }${ port }")
End Function
[C#]
String Extension(String url)
{
Regex r = new Regex(@"^(?
\w+)://[^/]+?(?
:\d+)?/",
RegexOptions.Compiled);
return r.Match(url).Result("${ proto }${ port }");
}
只有字母和數字,不小於6位,且數字字母都包含的密碼的正則表達式
在C#中,能夠用這個來表示:
一個將須要將路徑字符串拆分爲根目錄和子目錄兩部分的算法程序,考慮路徑格式有:C:\aa\bb\cc ,\\aa\bb\cc , ftp://aa.bb/cc 上述路徑將分別被拆分爲:C:\和aa\bb\cc ,\\aa 和 \bb\cc , ftp:// 和 aa.bb/cc 用javascript實現以下:
var
strRoot
,
strSub
var
regPathParse
=
/
^([^\\^
\/
]+[\\
\/
]+|\\\\[^\\]+)(.*)$
/
if
(
regPathParse
.
test
(
strFolder
))
{
strRoot
=
RegExp
.$
1
strSub
=
RegExp
.$
2
}