I have a function in my script that’s supposed to take in a string of HTML and return the same string with the exception that all the elements shall have been changed to one that is 2 levels higher (ie. h1->h3, h2->h4 etc.). This of cause needs to work independed of casing, and it must not remove attributes, however, I’m not about to use a full html-parser for it either, as it’s a fairly simple task, so I figured I’d go about this with regexes. The problem is (me beeing fairly new to vbscript and all) that I don’t know how to achieve the desired effect.
What I have currently is this:
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
What I want inside the For Each-loop is simply to swap the numbers, however, I’m not sure how to do that (I’m not even sure what properties the match-object exposes, and I’ve been unable to find it online).
How should I complete this function?
You’re asking for pain trying to do this with regex (not so much the replace but the fact that it’s an increment with a single regex pattern), if it’s only a case of replacing the Headers, i’d use replace():
(HTML Spec is only valid for
H1–H6– not sure if you want to ignoreH5&H6)If you want to stick with the regex option, i’d suggest the use of
regex.replace()I know in JavaScript you can pass the matched pattern into a function and use that function as the replacement, exactly what you would need here – but i’ve never seen this done in VBSCRIPT, example:
Use RegExp to match a parenthetical number then increment it
Edit 1:
Found the reference to the matches collection & match object:
http://msdn.microsoft.com/en-us/library/ms974570.aspx#scripting05_topic3
So, you could read the match from the match.value property, but you’d still need to resort to a 2nd replace i think