我的脚本中有一个函数,它应该接受一串HTML并返回相同的字符串,但所有元素都应该更改为高2级的函数(即.h1-> h3 ,h2-> h4等)。这个原因需要与外壳无关,并且它不能删除属性,但是,我也不打算使用完整的html-parser,因为这是一个相当简单的任务,所以我想我会去做这与正则表达式。问题是(我对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
- 循环中我想要的只是交换数字,但是,我不知道该怎么做(我甚至不确定match
- 对象的属性暴露,我一直无法在网上找到它。
我该如何完成此功能?
答案 0 :(得分:2)
你要求用正则表达式来做这个痛苦(不是替换,而是用单个正则表达式模式增加的事实),如果它只是替换Headers的情况,我会使用replace ():
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
有效 - 不确定是否要忽略H5
&amp; H6
)
如果你想坚持使用正则表达式选项,我建议使用regex.replace()
我知道在JavaScript中你可以将匹配的模式传递给一个函数并使用该函数作为替换函数,这正是你需要的 - 但我从来没有在VBSCRIPT中看到过这样的例子: Use RegExp to match a parenthetical number then increment it
修改1:
找到匹配集合的参考资料&amp;匹配对象:
http://msdn.microsoft.com/en-us/library/ms974570.aspx#scripting05_topic3
所以,你可以从match.value属性中读取匹配,但是你仍然需要求助于第二次替换我认为
答案 1 :(得分:0)
这是一个更通用的解决方案,可能是偏离主题的,因为它在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;