木子屋 Dnawo's BLOG

Javascript、VBscript和C#中使用正则表达式

👤 dnawo 📅 2006-12-01 👁 6121 👍 0 💬 0 🔄 本站原创
一、Javascript中使用正则表达式

创建:
var reg=/正则文本/[ig];
reg=new RegExp("正则文本",[ig]);
匹配:
reg.test(字符串) //返回布尔值是否匹配
替换:
字符串.replace(reg,替换字符)
举例:
1.匹配
<script language="javascript">
function mytest(){
	var reg,str,str2;
	reg = new RegExp("s$");
	str = "abcs";
	str2 = reg.test(str);
	alert(str2);
}
</script>
<input type="button" name="Submit" value="按钮" onClick="mytest();">

2.替换
<script language="javascript">
function mytest(){
	var reg,str,str2;
	reg = new RegExp("(s)");
	str = "abcs";
	str2 = str.replace(reg,"1122$1");
	alert(str2);
}
</script>
<input type="button" name="Submit" value="按钮" onClick="mytest();">

3.查找
<script language="javascript">
function  SubMatchTest(inpStr){
	var reg;
	reg = new RegExp("(\\w+)@(\\w+)\.(\\w+)","gi");
	var retStr,result;
	while((retStr = reg.exec(inpStr)) != null){
		result += "电子邮件地址是: " + retStr[0] + "\n";
		result += "电子邮件别名是: " + retStr[1] + "\n";
		result += "组织是: " + retStr[2] + "\n";
	}
	return result;
}
alert(SubMatchTest("请写信到 webmaster@mzwu.com 或 webmaster@163.com 。 谢谢!"));
</script>

二、VBscript中使用正则表达式

创建:
Set reg = new RegExp
reg.Pattern="正则文本"
reg.IgnoreCase= true '设置是否区分字符大小写
reg.Global=true '设置全局可用性
匹配:
reg.test(字符串) //返回布尔值是否匹配
替换:
reg.replace(字符串,替换字符)
查找:
reg.execute(字符串) //返回字符集

举例:
1.匹配
<script language="vbscript">
function mytest()
	Dim reg,str,str2
	Set reg = new RegExp
	reg.Pattern="s$"
	reg.IgnoreCase= true
	reg.Global=true
	str="abcs"
	str2=reg.test(str)
	msgbox str2
end function
</script>
<input type="button" name="Submit" value="按钮" onClick="mytest()">

2.替换
<script language="vbscript">
function mytest()
	Dim reg,str,str2
	Set reg = new RegExp
	reg.Pattern="s$"
	reg.IgnoreCase= true
	reg.Global=true
	str="abcs"
	str2=reg.replace(str,"1122")
	msgbox str2
end function
</script>
<input type="button" name="Submit" value="按钮" onClick="mytest()">

3.查找
<script language="vbscript">
Function SubMatchTest(inpStr)
	Dim regEx, oMatch, oMatches
	Set regEx = New RegExp
	regEx.Global = True
	regEx.IgnoreCase = True 
	regEx.Pattern = "(\w+)@(\w+)\.(\w+)"
	
	Set oMatches = regEx.Execute(inpStr)
	For each oMatch in oMatches
		retStr = "电子邮件地址是: " & oMatch & vbNewline
		retStr = retStr & "电子邮件别名是: " & oMatch.SubMatches(0) & vbNewline
		retStr = retStr & "组织是: " & oMatch. SubMatches(1) & vbNewline
		SubMatchTest = SubMatchTest & retStr
	Next

End Function
MsgBox(SubMatchTest("请写信到 webmaster@mzwu.com 或 webmaster@163.com 。 谢谢!"))
</script>

三、VB6.0使用正则表达式

VB6.0使用正则表达式语法和VBscript一样的,只不过使用前得先引用Microsoft VBscript Regular Expressions 5.5。

Dim regEx As RegExp
Dim oMatchs As MatchCollection
Dim oMatch As Match

Set regEx = New RegExp
regEx.Pattern = "([\w]+)@([\.\w]+)"
regEx.IgnoreCase = False
regEx.Global = True

Set oMatchs = regEx.Execute("请写信到 webmaster@mzwu.com 或 webmaster@163.com 。 谢谢!")
For Each oMatch In oMatchs
	MsgBox "电子邮件:" & oMatch.Value & vbCrLf & "用户名:" & oMatch.SubMatches(0) & vbCrLf & "所在域:" & oMatch.SubMatches(1)
Next

Set oMatch = Nothing
Set oMatchs = Nothing
Set regEx = Nothing

四、C#中使用正则表达式

匹配:
Regex.IsMatch(string input, string pattern, RegexOptions options) //返回布尔值是否匹配
替换:
Regex.Replace(string input, string pattern, string replacement, RegexOptions options)
查找:
Regex.Matches(string input, string pattern, RegexOptions options)
举例:
1.匹配
Regex.IsMatch("www.mzwu.com", "^mzwu$", RegexOptions.IgnoreCase)

2.替换
Regex.Replace("www.mzwu.com", "mzwu", "hao123", RegexOptions.IgnoreCase)

3.查找
//注意: 不能使用Regex.Match,它只返回第一个匹配结果
MatchCollection matches = Regex.Matches("One World,One Dream", "one", RegexOptions.IgnoreCase);
for (int i = 0; i < matches.Count; i++)
{
    Response.Write(matches[i].Value);
}

评论(0)

暂无评论。

计算题
评论需审核通过后显示
← 上一篇 innerText和innerHTML区别 下一篇 → 对动网的一个误解