请问:用vbs,利用正则表达式,怎样将html格式的文本中的文字加亮显示? 谢谢

yshawxp 2006-08-23 06:56:07
如题:

例如:有下面的HTML文本:
<b>大家<i><font color="red">爽</font></i></b>,<u>才是真的爽</u>
<img src="http://www.csdn.net/images/homeimage/csdn.gif">!

我想加亮显示"大家爽"这个文本,
因为这几个字夹杂有html标志,
我怎样才能在不破坏原来的html效果的前提下,加亮显示这个"大家爽"这几个文本呢?

就像这样的效果:
<span class="highlight"><b>大家<i><font color="red">爽</font></i></b></span>,<u>才是真的爽</u>
<img src="http://www.csdn.net/images/homeimage/csdn.gif">!



谢谢!
...全文
189 8 打赏 收藏 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yshawxp 2006-08-25
  • 打赏
  • 举报
回复
狼兄,謝謝你的回答!
不過你的函數還想請你幫我修改一下。
-----------------------------------
你的函數是:RegExpTest(strng),他只有一個參數阿,它只能把整個字符串strng加亮,

我想要的效果是:RegExpTest(html, key),應該是2個參數的,
在html裏面,搜索key,找到key之後,
在不破壞html文本的前提下,加亮key。

如題,

html:
<b>大家<i><font color="red">爽</font></i></b>,<u>才是真的爽</u>
<img src="http://www.csdn.net/images/homeimage/csdn.gif">!
------------------------------------------------
key:大家爽
------------------------------------------------
結果:
<span class="highlight"><b>大家<i><font color="red">爽</font></i></b></span>,<u>才是真的爽</u>
<img src="http://www.csdn.net/images/homeimage/csdn.gif">!


請你幫幫我,老大催我了!
謝謝!





  • 打赏
  • 举报
回复
asp啊,那还真不好办了....你咋知道大家爽这几个字的??
  • 打赏
  • 举报
回复
document.execCommand

对当前的selection进行一下改变
飞驰的青蛙 2006-08-23
  • 打赏
  • 举报
回复
你研究一下文本编辑器,如:ewebeditor。当选中文本后,是怎样改变文本的颜色,大小等的。
jspadmin 2006-08-23
  • 打赏
  • 举报
回复
replace替换?
如果只是加这一句可以这么做,批量的话,就不太好弄了
超级大笨狼 2006-08-23
  • 打赏
  • 举报
回复
稍微修改了一下:

<textArea id="blockORG"><b>大家<i><font color="red">爽</font></i></b>,<u>才是真的爽</u>
<img src="http://www.csdn.net/images/homeimage/csdn.gif">!</textArea>

<SCRIPT LANGUAGE="vbScript">

alert(RegExpTest( blockORG.value))

Function RegExpTest(strng)
Dim regEx,patrn, Match, Matches,pos,i ' 创建变量。
Set regEx = New RegExp ' 创建正则表达式。
patrn="<[^<>]*>"
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
regEx.Global = True ' 设置全程匹配。
strng="<p>" & strng & "</p>"
Set Matches = regEx.Execute(strng) ' 执行搜索。
pos=0
i=0

Dim d :Set d = CreateObject("Scripting.Dictionary")
For Each Match in Matches ' 循环遍历Matches集合。

if pos<>Match.FirstIndex then
i=i+1
d.add i ,"<span style='color:red'>" & mid(strng,pos +1,Match.FirstIndex-pos) & "</span>"
end if
i=i+1
d.add i,mid( strng , Match.FirstIndex+1 , len(Match.Value))
pos=Match.FirstIndex + len(Match.Value)
Next
for each str in d.items()
RetStr = RetStr & str
next
RegExpTest = RetStr
End Function

</SCRIPT>

超级大笨狼 2006-08-23
  • 打赏
  • 举报
回复
<textArea id="blockORG"><b>大家<i><font color="red">爽</font></i></b>,<u>才是真的爽</u>
<img src="http://www.csdn.net/images/homeimage/csdn.gif">!</textArea>
<SCRIPT LANGUAGE="vbScript">
alert(RegExpTest("<[^<>]*>",blockORG.value))
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' 创建变量。
Set regEx = New RegExp ' 创建正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
regEx.Global = True ' 设置全程匹配。
strng="<p>" & strng & "</p>"
Set Matches = regEx.Execute(strng) ' 执行搜索。
pos=0
i=0

Dim d :Set d = CreateObject("Scripting.Dictionary")
For Each Match in Matches ' 循环遍历Matches集合。

if pos<>Match.FirstIndex then
i=i+1
d.add i ,"<span style='color:red'>" & mid(strng,pos +1,Match.FirstIndex-pos) & "</span>"
end if
i=i+1
d.add i,mid( strng , Match.FirstIndex+1 , len(Match.Value))
pos=Match.FirstIndex + len(Match.Value)
Next
for each str in d.items()
RetStr = RetStr & str
next
RegExpTest = RetStr
End Function

</SCRIPT>



结果:


---------------------------
Microsoft Internet Explorer
---------------------------
<p><b><span style='color:red'>大家</span><i><font color="red"><span style='color:red'>爽</span></font></i></b><span style='color:red'>,</span><u><span style='color:red'>才是真的爽</span></u><span style='color:red'>

</span><img src="http://www.csdn.net/images/homeimage/csdn.gif"><span style='color:red'>!</span></p>
---------------------------
确定
---------------------------

  • 打赏
  • 举报
回复
js你直接弄文本编辑器不就得了??

28,402

社区成员

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

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