html - 使用经典 ASP 替换 HTML 中的文本

标签 html asp-classic replace

我不确定使用 Classic ASP (VBscript) 是否可行,但我想做的是:

在 HTML 文本中搜索特定字符串(仅在 p-tags、div-tags、li-tags、span-tags 之间并忽略其他标签(如 a-tags 之间的文本)并将其替换为其他内容。例如:

原始 HTML

<p>Some text with the keyword.</p>

经典 ASP 函数

仅在所有 p-tags、div-tags、span-tags 和 li-tags 之间搜索 HTML 文本中的关键字,而忽略其他标签(如 a-tags)之间的文本。将关键字替换为其他内容。

输出 HTML

<p>Some text with the <a href="someurl">keyword</a>.</p>

如果这不可能可能,还有其他方法吗? 有没有人有这方面的经验?

只是为了清楚。 “一些带有关键字的文本。”文本不是来自数据库,因为那样太容易了 :) 不幸的是,它位于 .asp 文件的 HTML 中。

最佳答案

这里需要进行字符串操作。因为我从来没有真正接触过正则表达式,所以很久以前我就开始使用我自己的方法来以各种方式操作文本。非常丑陋,不是最佳效率,但它有效。

首先,将这些函数添加到您的代码中:

Function GetBetween(str, leftDelimeter, rightDelimeter)
    Dim tmpArr, result(), x
    tmpArr=Split(str, leftDelimeter)
    If UBound(tmpArr) < 1 Then
        GetBetween=Array() : Exit Function
    End If
    ReDim result(UBound(tmpArr)-1)
    For x=1 To UBound(tmpArr)
        result(x-1)=(Split(tmpArr(x), rightDelimeter))(0)
    Next
    Erase tmpArr
    GetBetween=result
End Function

Function ReplaceWholeWord(ByVal strText, strWord, strToReplaceWith)
    ReplaceWholeWord = strText
    If InStr(strText, strWord)<1 Then Exit Function

    'Text is only the word?
    If strText=strWord Then
        strText = Replace(strText, strWord, strToReplaceWith)
    Else  
        'Text starting with word?
        If InStr(strText, strWord & " ")=1 Then
            strText = strToReplaceWith & " " & Right(strText, Len(strText) - Len(strWord & " "))
        End If

        'Text ends with word?
        If Right(strText, Len(" " & strWord))=(" " & strWord) Then
            strText = Left(strText, Len(strText) - Len(" " & strWord)) & " " & strToReplaceWith
        End If

        'Text ends with word and a period?
        If Right(strText, Len(" " & strWord & "."))=(" " & strWord & ".") Then
            strText = Left(strText, Len(strText) - Len(" " & strWord & ".")) & " " & strToReplaceWith & "."
        End If

        'Replace between other words:
        strText = Replace(strText, " " & strWord & " ", " " & strToReplaceWith & " ")
    End If

    ReplaceWholeWord = strText
End Function

有了这些功能,下面是按照您想要的方式替换关键字的示例代码:

Const KEYWORD = "keyword"
Dim strHTML, arrTagsToFind, x
Dim curItemsArray,  y, curItem
Dim curReplacedItem
arrTagsToFind = Array("p", "div")
strHTML = "<b>this keyword will not be replaced</b><p>Some text with the keyword.</p>keyword here won't be replaced too<p>Some other text with the keyword22 that is not whole word but end with keyword</p><div>keyword first</div>"
For x=0 To UBound(arrTagsToFind)
    curItemsArray = GetBetween(strHTML, "<" + arrTagsToFind(x) + ">", "</" + arrTagsToFind(x) + ">")
    For y=0 To UBound(curItemsArray)
        curItem = curItemsArray(y)
        curReplacedItem = ReplaceWholeWord(curItem, KEYWORD, "<a href=""#foo"">" & KEYWORD & "</a>")
        strHTML = Replace(strHTML, curItem, curReplacedItem)
    Next
Next
Response.Write(strHTML)

关于html - 使用经典 ASP 替换 HTML 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13364164/

相关文章:

JavaScript:检查 window.location.href 是否已更改

javascript - 将硬编码行添加到 AngularJS 表选项

asp-classic - 经典 ASP : run some code at the start of every request before processing actual . asp 文件

PHP字符串替换匹配整个单词

java - 如何在java中替换["with "“

javascript - 以 HTML 格式直接输出 PHP 代码

html - 以从右到左的语言从左到右显示电话号码

c# - 从 EPS 文件中提取 XMP 元数据

sql - 如何修复错误过程需要 '@parameters' 类型的参数 'ntext/nchar/nvarchar' ?

c - 如何从 C 中的字符串中去除文件扩展名?