摘要:收集一些常用的正則表達(dá)式。
正則表達(dá)式用于字符串處理,表單驗(yàn)證等場(chǎng)合,實(shí)用高效,但用到時(shí)總是不太把握,以致往往要上網(wǎng)查一番。我將一些常用的表達(dá)式收藏在這里,作備忘之用。本貼隨時(shí)會(huì)更新。
匹配中文字符的正則表達(dá)式:
匹配雙字節(jié)字符(包括漢字在內(nèi)):
應(yīng)用:計(jì)算字符串的長(zhǎng)度(一個(gè)雙字節(jié)字符長(zhǎng)度計(jì)2,ASCII字符計(jì)1)
String
.
prototype
.
len
=
function
(){
return
this
.
replace
([
^\
x00
-\
xff
]
/
g,"aa").length; }
匹配空行的正則表達(dá)式:
匹配HTML標(biāo)記的正則表達(dá)式:
/
<(.*)>.*<
\/
\1>|<(.*)
\/
>
/
匹配首尾空格的正則表達(dá)式:
應(yīng)用:j avascript中沒有像v bscript那樣的trim函數(shù),我們就可以利用這個(gè)表達(dá)式來實(shí)現(xiàn),如下:
String
.
prototype
.
trim
=
function
()
{
?? ?
return
this
.
replace
(
/
(^\s*)|(\s*$)
/g
,
""
)
;
}
利用正則表達(dá)式分解和轉(zhuǎn)換IP地址
下面是利用正則表達(dá)式匹配IP地址,并將IP地址轉(zhuǎn)換成對(duì)應(yīng)數(shù)值的Javascript程序:
function
IP2V
(
ip
)
{
?? ?
re
=
/
(\d+)\.(\d+)\.(\d+)\.(\d+)
/g
?
//匹配IP地址的正則表達(dá)式
?? ?
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!
"
)
?? ?
}
}
不過上面的程序如果不用正則表達(dá)式,而直接用split函數(shù)來分解可能更簡(jiǎn)單,程序如下:
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地址的正則表達(dá)式:
\
w
+
([
-+.
]
\
w
+
)
*@\
w
+
([
-.
]
\
w
+
)
*\.\
w
+
([
-.
]
\
w
+
)
*
匹配網(wǎng)址URL的正則表達(dá)式:
http
:
//([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
利用正則表達(dá)式去除字串中重復(fù)的字符的算法程序:[*注:此程序不正確]
var
s
=
"
abacabefgeeii
"
var
s1
=
s
.
replace
(
/
(.).*\1
/g
,
"
$1
"
)
var
re
=
new
RegExp
(
"
[
"
+
s1
+
"
]
"
,
"
g
"
)
var
s2
=
s
.
replace
(
re
,
""
)
alert
(
s1
+
s2
)
?
//結(jié)果為:abcefgi
*注
===============================
如果var s = “abacabefggeeii”
結(jié)果就不對(duì)了,結(jié)果為:abeicfgg
正則表達(dá)式的能力有限
===============================
我原來在CSDN上發(fā)貼尋求一個(gè)表達(dá)式來實(shí)現(xiàn)去除重復(fù)字符的方法,最終沒有找到,這是我能想到的最簡(jiǎn)單的實(shí)現(xiàn)方法。思路是使用后向引用取出包括重復(fù)的字符,再以重復(fù)的字符建立第二個(gè)表達(dá)式,取到不重復(fù)的字符,兩者串連。這個(gè)方法對(duì)于字符順序有要求的字符串可能不適用。
得用正則表達(dá)式從URL地址中提取文件名的javascript程序,如下結(jié)果為page1
s
=
"
http://blog.penner.cn/page1.htm
"
s
=
s
.
replace
(
/
(.*
\/
){ 0, }([^\.]+).*
/i
g
,
"
$2
"
)
alert
(
s
)
利用正則表達(dá)式限制網(wǎng)頁(yè)表單里的文本框輸入內(nèi)容:
用正則表達(dá)式限制只能輸入中文:
onkeyup
=
"
value=value.replace(/[^\u4E00-\u9FA5]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\u4E00-\u9FA5]/g,''))
"
用正則表達(dá)式限制只能輸入全角字符:
onkeyup
=
"
value=value.replace(/[^\uFF00-\uFFFF]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\uFF00-\uFFFF]/g,''))
"
用正則表達(dá)式限制只能輸入數(shù)字:
onkeyup
=
"
value=value.replace(/[^\d]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))
"
用正則表達(dá)式限制只能輸入數(shù)字和英文:
onkeyup
=
"
value=value.replace(/[\W]/g,'')
"
onbeforepaste
=
"
clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))
"
匹配非負(fù)整數(shù)(正整數(shù) + 0)
匹配正整數(shù)
^
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*$
匹配非正整數(shù)(負(fù)整數(shù) + 0)
^
((
-\
d
+
)
|
(
0
+
))
$
匹配負(fù)整數(shù)
^-
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*$
匹配整數(shù)
匹配非負(fù)浮點(diǎn)數(shù)(正浮點(diǎn)數(shù) + 0)
匹配正浮點(diǎn)數(shù)
^
(([
0
-
9
]
+\.
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*\.
[
0
-
9
]
+
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
))
$
匹配非正浮點(diǎn)數(shù)(負(fù)浮點(diǎn)數(shù) + 0)
^
((
-\
d
+
(
\.\
d
+
)
?
)
|
(
0
+
(
\
.0
+
)
?
))
$
匹配負(fù)浮點(diǎn)數(shù)
^
(
-
(([
0
-
9
]
+\.
[
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*\.
[
0
-
9
]
+
)
|
([
0
-
9
]
*
[
1
-
9
][
0
-
9
]
*
)))
$
匹配浮點(diǎn)數(shù)
^
(
-?\
d
+
)(
\.\
d
+
)
?$
匹配由26個(gè)英文字母組成的字符串
匹配由26個(gè)英文字母的大寫組成的字符串
匹配由26個(gè)英文字母的小寫組成的字符串
匹配由數(shù)字和26個(gè)英文字母組成的字符串
匹配由數(shù)字、26個(gè)英文字母或者下劃線組成的字符串
匹配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.確認(rèn)有效電子郵件格式
下面的示例使用靜態(tài) Regex.IsMatch 方法驗(yàn)證一個(gè)字符串是否為有效電子郵件格式。如果字符串包含一個(gè)有效的電子郵件地址,則 IsValidEmail 方法返回 true,否則返回 false,但不采取其他任何操作。您可以使用 IsValidEmail,在應(yīng)用程序?qū)⒌刂反鎯?chǔ)在數(shù)據(jù)庫(kù)中或顯示在 ASP.NET 頁(yè)中之前,篩選出包含無效字符的電子郵件地址。
[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.清理輸入字符串
下面的代碼示例使用靜態(tài) Regex.Replace 方法從字符串中抽出無效字符。您可以使用這里定義的 CleanInput 方法,清除掉在接受用戶輸入的窗體的文本字段中輸入的可能有害的字符。CleanInput 在清除掉除 @、-(連字符)和 .(句點(diǎn))以外的所有非字母數(shù)字字符后返回一個(gè)字符串。
[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(?<month>\d{ 1,2 })/(?<day>\d{ 1,2 })/(?<year>\d{ 2,4 })\b", _
"${ day }-${ month }-${ year }")
End Function
[C#]
String MDYToDMY(String input)
{
?? ?return Regex.Replace(input,"\\b(?<month>\\d{ 1,2 })/(?<day>\\d{ 1,2 })/(?<year>\\d{ 2,4 })\\b","${ day }-${ month }-${ year }");
}
Regex 替換模式
本示例說明如何在 Regex.Replace 的替換模式中使用命名的反向引用。其中,替換表達(dá)式 ${ day } 插入由 (?…) 組捕獲的子字符串。
有幾種靜態(tài)函數(shù)使您可以在使用正則表達(dá)式操作時(shí)無需創(chuàng)建顯式正則表達(dá)式對(duì)象,而 Regex.Replace 函數(shù)正是其中之一。如果您不想保留編譯的正則表達(dá)式,這將給您帶來方便
4.提取 URL 信息
以下代碼示例使用 Match.Result 來從 URL 提取協(xié)議和端口號(hào)。例如,“http://www.penner.cn:8080……將返回“http:8080”。
[Visual Basic]
Function Extension(url As String) As String
Dim r As New Regex("^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", _
RegexOptions.Compiled)
Return r.Match(url).Result("${ proto }${ port }")
End Function
[C#]
String Extension(String url)
{
?? ?Regex r = new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/",
?? ?RegexOptions.Compiled);
?? ?return r.Match(url).Result("${ proto }${ port }");
}
只有字母和數(shù)字,不小于6位,且數(shù)字字母都包含的密碼的正則表達(dá)式
在C#中,可以用這個(gè)來表示:
一個(gè)將需要將路徑字符串拆分為根目錄和子目錄兩部分的算法程序,考慮路徑格式有:C:\aa\bb\cc ,\\aa\bb\cc , ftp://aa.bb/cc 上述路徑將分別被拆分為:C:\和aa\bb\cc ,\\aa 和 \bb\cc , ftp:// 和 aa.bb/cc 用javascript實(shí)現(xiàn)如下:
var
strRoot
,
strSub
var
regPathParse
=
/
^([^\\^
\/
]+[\\
\/
]+|\\\\[^\\]+)(.*)$
/
if
(
regPathParse
.
test
(
strFolder
))
{
?? ?
strRoot
=
RegExp
.$
1
?? ?
strSub
=
RegExp
.$
2
}
Tags:?
JavaScript?正則表達(dá)式?This entry was posted on 星期二, 三月 22nd, 2005 at 11:12:25 and is filed under Tech, JavaScript. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.
posted on 2007-03-16 17:08
xzc 閱讀(444)
評(píng)論(0) 編輯 收藏 所屬分類:
XML