【问题标题】:Decreasing all headings in classic ASP减少经典 ASP 中的所有标题
【发布时间】:2026-02-12 06:35:01
【问题描述】:

我的脚本中有一个函数,它应该接收一个 HTML 字符串并返回相同的字符串,但所有元素都应更改为高 2 级的元素(即 h1->h3, h2->h4 等)。这个原因需要独立于大小写,并且它不能删除属性,但是,我也不打算使用完整的 html 解析器,因为这是一个相当简单的任务,所以我想我会去这与正则表达式。问题是(我对 vbscript 很陌生)我不知道如何达到预期的效果。

我目前拥有的是这样的:

Function fiksoverskrifter(html)
   Dim regex, matches, match
   Set regex = New RegExp
   regex.Pattern = "<(/?)h([0-9])(.*?)>"
   regex.IgnoreCase = True
   regex.Multiline = False

   fiksoverskrifter = html

   Set matches = regex.Execute(html)
   For Each match in matches

   Next

   Set regex = Nothing
   Set matches = Nothing
   Set match = Nothing
End Function

我在For Each-loop 中想要的只是交换数字,但是,我不知道该怎么做(我什至不确定match-object 暴露了哪些属性,我'一直无法在网上找到它)。

我应该如何完成这个功能?

【问题讨论】:

    标签: html regex asp-classic replace


    【解决方案1】:

    如果只是替换标题的情况,我会使用 regex ():

    For i = 4 To 1 Step -1
        strHtml = replace(strHtml, "<h" & cstr(i), "<h" & cstr(i + 2), 1, -1, vbTextCompare)
        strHtml = replace(strHtml , "</h" & cstr(i), "</h" & cstr(i + 2), 1, -1, vbTextCompare)
    Next
    

    (HTML 规范仅对 H1-H6 有效 - 不确定是否要忽略 H5H6

    如果你想坚持使用正则表达式选项,我建议使用regex.replace()

    我知道在 JavaScript 中你可以将匹配的模式传递给一个函数并使用该函数作为替换,这正是你在这里需要的——但我从未见过在 VBSCRIPT 中这样做过,例如: Use RegExp to match a parenthetical number then increment it

    编辑 1:

    找到对匹配集合和匹配对象的引用:

    http://msdn.microsoft.com/en-us/library/ms974570.aspx#scripting05_topic3

    因此,您可以从 match.value 属性中读取匹配项,但我认为您仍然需要使用第二次替换

    【讨论】:

    • 我必须说,你不能这样做。原因,然后你会用 h3 替换 h1,然后在循环中用 h5 替换 h3 xD
    • @Alxandr:轻松修复使用For 4 To 1 Step -1
    • 此外,您在行尾缺少),并且您在不忽略大小写的情况下进行了比较,并且您也从未将结果分配给任何xD。我将您的代码更新为可以在其他人需要时使用的代码。
    【解决方案2】:

    这里有一个更通用的解决方案,不过可能离题了,因为它是在 Perl 中,而不是在 VBScript 中。请注意,我记录了它以对抗正则表达式往往具有的只写效果。

    C:\TEMP :: more /t4 hdradj.pl
    use strict;
    use warnings;
    
    # Make a subroutine that will adjust HTML headers
    # (like h1, H2) by doing string replacement.
    # Will only work properly for the range from 1 to 9.
    sub mk_header_adjuster {
        my( $adjustment, $lower, $upper ) = @_;
    # Compile substitution expression to adjust headers like h1, H2.
    # Left-hand side matches headers from the range specified.
    # Uses word boundaries (\b) and a character range (square brackets).
    # Captures matches for "h|H" in $1 and for the level in $2.
    # Right-hand side uses an eval (e-switch) to compute substitution.
    # Case is ignored (i-switch), replacement is global (g-switch).
    # Wraps expression in subroutine to modify first argument.
        my $str = <<"EOSUB";
    sub {
        \$_[0] =~ s/\\b(h)([$lower-$upper])\\b/\$1 . (\$2 + $adjustment)/ige
    }
    EOSUB
    #   print $str, "\n"; # debug output
        my $sub = eval $str; # compile expression
        die $@ if $@; # abort in case of errors in eval compilation
        return $sub;
    }
    
    # ==== main ====
    # Test the above subroutine is working properly.
    my $test_input = <<'EOS';
    <h1>eins</h1>
    <p>...
    <h2 bla="blub">eins blub</h2>
    < H2 >zwei </ H2>
    <h3 >drei </h3>
    <h4>vier </h4>
    <h5>fünf </h5>
    EOS
    
    # Compile a header adjuster moving headers 2 to 4 by 2 levels down.
    my $adjuster = mk_header_adjuster 2, 2, 4;
    my $number_of_replacements = $adjuster->( $test_input );
    printf STDERR
    "Replaced %u header tags in supplied input.\n", $number_of_replacements;
    print $test_input;
    

    【讨论】:

    • 当我的主要问题是正则表达式在 vbs 中的工作方式时,这有什么帮助? o.O
    • @Alxandr - 好吧,我认为您的主要问题是转换 HTML 文件。很高兴知道这是另一回事。