一个正则。。。难

ootwo 2013-02-19 11:30:55
	function replaceNotInHref(str, word, reword, isGlobal)
dim re
set re = new RegExp
re.Pattern = word & "(?:(?=.*<a\b)|(?!.*?<\/a>))"
re.IgnoreCase = True

re.Global = isGlobal

replaceNotInHref = re.replace(str, reword)
end function

str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属"
response.write replaceNotInHref(str, "金属", "被替换了", false)


怎么替换A标签之外的字符
...全文
306 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
打字员 2013-02-20
  • 打赏
  • 举报
回复
一個有點笨的方法: 先把不需要換的地方換掉,再換需要的地方,最后再把不需要換的地方換回來

	var str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属";
	str = str.replace(/(<a[^>]+>.*?)金属(.*?<\/a>)/ig, "$1xxoo$2").replace(/金属/g, '我暈!').replace(/xxoo/ig, "金属");
	alert(str);
也可以簡化一下,先不管那么多,都換掉,然后再把不需要換的地方換回來 看你程序怎麼做更合適
ootwo 2013-02-19
  • 打赏
  • 举报
回复
如果是
replaceNotInHref(str, "金属", "被替换了", true) 


得到的是
123<a href='f'>金属123</a>被替换了123<a href='f'>金属</a>123被替换了
ootwo 2013-02-19
  • 打赏
  • 举报
回复
上面例子想得到
123<a href='f'>金属123</a>被替换了123<a href='f'>金属</a>123金属
001007009 2013-02-19
  • 打赏
  • 举报
回复
参考下
<%
	function replaceNotInHref(str, word, reword, isGlobal)
		dim re, i, Matches, Match
		set re = new RegExp
		re.IgnoreCase = true
		re.Global = true
		re.Pattern = "(?:<a.*?>[\s\S]*?</a>)"

		Set Matches =re.Execute(str)
		
		i = 0
		For Each Match in Matches	
			ReDim Preserve MyArray(i)
			MyArray(i) = Match.Value
			str = replace(str, Match.Value, "<@#"&i&"#@>")'
			i = i + 1
		Next
		
		re.Global = isGlobal
		re.Pattern = word
		
		str = re.replace(str, reword)
		
		if i > 0 then
			for i = 0 to ubound(MyArray)
				str = replace(str, "<@#"&i&"#@>", MyArray(i))
			next
		end if
		replaceNotInHref = str
	end function

	str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属"
	str1 = "123金属123金属1233"
	response.write replaceNotInHref(str, "金属", "被替换了", false)
	response.write "<br>"
	response.write replaceNotInHref(str1, "金属", "被替换了", true)
%>
ootwo 2013-02-19
  • 打赏
  • 举报
回复
引用 8 楼 foolbirdflyfirst 的回复:
#5去掉正则式里的'|$'可满足#1需求。
	function replaceNotInHref(str, word, reword, isGlobal)
		dim re
		set re = new RegExp
		're.Pattern = "(?=[^>]*(?=<(?!\/)|$))" & word
		re.Pattern = "(?=[^>]*(?=<(?!\/)|$))" & word
		re.IgnoreCase = True
		
		re.Global = isGlobal
		
		replaceNotInHref = re.replace(str, reword)
	end function
	
	str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属"
	str1 = "123金属123金属1233"
	response.write replaceNotInHref(str, "金属", "被替换了", false)
	response.write "<br>"
	response.write replaceNotInHref(str1, "金属", "被替换了", true)
还有没有a的情况,,
foolbirdflyfirst 2013-02-19
  • 打赏
  • 举报
回复
#5去掉正则式里的'|$'可满足#1需求。
Kimshuen 2013-02-19
  • 打赏
  • 举报
回复
不要Global就ok了吧
bbjbepzz 2013-02-19
  • 打赏
  • 举报
回复
LZ写的这代码冒似不是JS吧,看起来像VB。
foolbirdflyfirst 2013-02-19
  • 打赏
  • 举报
回复
var str = "123<a href='f'>金属123</a>金属123<a href='f'>金属</a>123金属";
alert(str.replace(/(?=[^>]*(?=<(?!\/)|$))金属/g,'橡胶'))
laoguofly 2013-02-19
  • 打赏
  • 举报
回复
得到的是这个<a href='f'>金属123</a><a href='f'>金属</a> 你要替换外边的捕获外边的就好了。
laoguofly 2013-02-19
  • 打赏
  • 举报
回复
\w*(<a[^>]+>[^<]*</a>)\w* 然后将捕获的字符替换进去就好了。

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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