When using the editor, you will encounter one point, that is, the closing problem of tags. This problem is very serious because it may cause the overall style of the web page to be displayed to be damaged. I recently saw this function in PJ's function library. I feel that the idea is a bit poor, but it is relatively perfect. It's just a matter of the order when closing the tag, haha.
Modify the contents of each element in the array arrTags to achieve the function of closing any tag.
Here, I have added some comments to facilitate everyone to study together.
Copy the code code as follows:
Function closeUBB(strContent)
'************************************
'Automatically close UBB
'************************************
Dim arrTags, i, OpenPos, ClosePos, re, strMatchs, j, Match
Set re = New RegExp 'Declare the re object
re.IgnoreCase = True 'Set whether characters are case-sensitive
re.Global = True 'Set global availability
arrTags = Array(code, quote, list, color, align, font, size, b, i, u, html) 'Create an array and store related tags that need to be checked for closure
For i = 0 To UBound(arrTags) 'Loop to detect each element in the array
OpenPos = 0 'Initialize the number of start tags of the current label
ClosePos = 0 'Initialize 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
Notes on closehtml are the same as above
Copy the code code 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