This function was recently seen in PJ's function library. It seems that the idea is a little bad, but it is relatively perfect, but it is just a sequence problem when the tags are closed.
Modify the content of each element in the array arrTags to close any tag.
Here, I have added some notes to help you learn together.
Copy codeThe Code is as follows: Function closeUBB (strContent)
'*************************************
'Automatically closes UBB
'*************************************
Dim arrTags, I, OpenPos, ClosePos, re, strMatchs, j, Match
Set re = New RegExp 'declares the re object
Re. IgnoreCase = true': Specifies whether to distinguish between uppercase and lowercase characters.
Re. Global = true' sets Global availability
ArrTags = Array ("code", "quote", "list", "color", "align", "font", "size", "B", "I ", "u", "html") 'creates an array to store related tags that need to be checked for closure
For I = 0 To UBound (arrTags) 'checks each element in the logarithm group cyclically.
OpenPos = 0' initialize the number of tags starting with the current tag
ClosePos = 0' initializes the number of end tags of the current tag
Re. pattern = "\ [" + arrTags (I) + "(= [^ \ [\] + |) \]" 'start to judge the number of start and end tags respectively
Set strMatchs = re. Execute (strContent)
For Each Match in strMatchs
OpenPos = OpenPos + 1
Next
Re. Pattern = "\ [/" + arrTags (I) + "\]"
Set strMatchs = re. Execute (strContent)
For Each Match in strMatchs
ClosePos = ClosePos + 1
Next
For j = 1 To OpenPos-closepos' when the number of start and end tags is inconsistent, close the current tag
StrContent = strContent + "[/" + arrTags (I) + "]"
Next
Next
CloseUBB = strContent
Set re = Nothing
End Function
Closehtml comments are the same as above.Copy codeThe Code is as follows: Function closehtml (strContent)
'*************************************
'Automatically close html
'*************************************
Dim arrTags, I, OpenPos, ClosePos, re, strMatchs, j, Match
Set re = New RegExp
Re. IgnoreCase = True
Re. Global = True
ArrTags = Array ("p", "DIV", "span", "table", "ul", "font", "B", "u", "I ", "h1", "h2", "h3", "h4", "h5", "h6 ")
For I = 0 To UBound (arrTags)
OpenPos = 0
ClosePos = 0
Re. Pattern = "\ <" + arrTags (I) + "([^ \ <\>] + |) \>"
Set strMatchs = re. Execute (strContent)
For Each Match in strMatchs
OpenPos = OpenPos + 1
Next
Re. Pattern = "\ </" + arrTags (I) + "\>"
Set strMatchs = re. Execute (strContent)
For Each Match in strMatchs
ClosePos = ClosePos + 1
Next
For j = 1 To OpenPos-ClosePos
StrContent = strContent + "</" + arrTags (I) + ">"
Next
Next
Closehtml = strContent
Set re = Nothing
End Function