不错呦!smile@林凯西,确保“准备文件”中的几个文件都有安装,S...您好,看了您这篇帖子觉得很有帮助。但是有个问题想请...我的修改过了怎么还被恶意注册呢 @jjjjiiii 用PJ快9年了,主要是A...PJ3啊,貌似很少有人用PJ了,现在不是WP就是z...@332347365,我当时接入时错误码没有-10...楼主,ChkValue值应为-103是什么意思呢?...大哥 你最近能看到我发的信息,请跟我联系,我有个制...
Javascript、VBscript和C#中使用正则表达式
编辑:dnawo 日期:2006-12-01
一、Javascript中使用正则表达式
创建:
var reg=/正则文本/[ig];
reg=new RegExp("正则文本",[ig]);
匹配:
reg.test(字符串) //返回布尔值是否匹配
替换:
字符串.replace(reg,替换字符)
举例:
1.匹配
2.替换
3.查找
二、VBscript中使用正则表达式
创建:
Set reg = new RegExp
reg.Pattern="正则文本"
reg.IgnoreCase= true '设置是否区分字符大小写
reg.Global=true '设置全局可用性
匹配:
reg.test(字符串) //返回布尔值是否匹配
替换:
reg.replace(字符串,替换字符)
查找:
reg.execute(字符串) //返回字符集
举例:
1.匹配
2.替换
3.查找
三、VB6.0使用正则表达式
VB6.0使用正则表达式语法和VBscript一样的,只不过使用前得先引用Microsoft VBscript Regular Expressions 5.5。
四、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.匹配
2.替换
3.查找
创建:
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();">
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();">
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>
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()">
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()">
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>
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
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);
}
MatchCollection matches = Regex.Matches("One World,One Dream", "one", RegexOptions.IgnoreCase);
for (int i = 0; i < matches.Count; i++)
{
Response.Write(matches[i].Value);
}
评论: 1 | 引用: 0 | 查看次数: 5486
发表评论
请登录后再发表评论!