如何使用正则表达式截取字符串

helloclm 2007-09-13 05:35:02
有字符串a大约100字,
确定字符串b在a中位置后,
如何用正则获取b位置之前2位到b位置之后2位之间的文字?

如:
a="...qwesflkaea===ekfjaew;fkj..."
b="==="
我该如何用正则获得"ea===ek" ?

谢谢。
...全文
217 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
cattle13 2007-09-14
  • 打赏
  • 举报
回复
a="...qwesflkaea===ekfjaew;fkj..."
b="==="
pat="[\s\S]*(.{2}" & b & ".{2})[\s\S]*"
function RegExpTest(str,pat)
Dim r,name
Set r = New RegExp
r.Pattern = pat
r.IgnoreCase = false
r.Global = true
RegExpTest=r.replace(str,"$1")
End function
response.write RegExpTest(a,pat)
helloclm 2007-09-14
  • 打赏
  • 举报
回复
谢谢,需要正则。
dnawo 2007-09-14
  • 打赏
  • 举报
回复
<%
Dim a,b
Dim regEx

a = "...qwesflkaea===ekfjaew;fkj..."
b = "==="

Set regEx = New RegExp
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = "[\S\s]{2}" & b & "[\S\s]{2}"

Response.Write(regEx.Execute(a)(0))
%>
dnawo 2007-09-14
  • 打赏
  • 举报
回复
<%
Dim a,b
Dim regEx

a = "...qwesflkaea===ekfjaew;fkj..."
b = "==="

Set regEx = New RegExp
regEx.Global = True
regEx.IgnoreCase = True
regEx.Pattern = "[\S\s]{2}===[\S\s]{2}"

Response.Write(regEx.Execute(a)(0))
%>
dnawo 2007-09-13
  • 打赏
  • 举报
回复
不用正则也可以实现:

<%
a="...qwesflkaea===ekfjaew;fkj..."
b="==="

Response.Write(Mid(a,Instr(a,b)-2,Len(b)+4))
%>

回家了,要正则明天再贴
cnfreer 2007-09-13
  • 打赏
  • 举报
回复
alert('...qwesflkaea===ekfjaew;fkj...'.replace(/^.*(.{2}===.{2}).*$/, '$1'));

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧