<%Class TLeft
Private c_Max, c_o, c_n, c_c, c_x, c_s
Private c_d, c_a, c_r
Private Sub Class_Initialize()
'c_Max 控制查找最大数
'c_o 控制是否继续查找
'c_n 记录字数
'c_c 记录未结束标记数
'c_x 无效未结束标记数
'c_s 预处理的String
'c_d 记录所有没有结尾的标记
'c_a 记录所有匹配出的内容
'c_r 公用正则对象
c_Max = 0
Set c_d = Server.CreateObject("Scripting.Dictionary")
Set c_a = Server.CreateObject("Scripting.Dictionary")
Set c_r = new RegExp
End Sub
Private Sub Class_Terminate
c_d.RemoveAll : Set c_d = Nothing
c_a.RemoveAll : Set c_a = Nothing
Set c_r = Nothing
End Sub
Private Sub Sd()
'set d
Dim m, i
c_r.Pattern = "<[^>]+\/>"
c_r.Global = True
Set m = c_r.Execute(c_s)
For i = 0 To m.Count - 1
c_d.Add i, m(i).Value
Next
c_s = c_r.Replace(c_s, "を")
End Sub
Private Function Ss()
'scan string
Dim a, i, s
s = toString(c_a) : a = Split(s, "を") : s = ""
For i = 0 To UBound(a)
s = s & a(i)
If i < UBound(a) Then s = s & c_d.Item(i)
Next
Ss = s
End Function
Private Function toString(o)
'dic toString
Dim a, i, s
a = o.Keys
For i = 0 To o.Count - 1
s = s & o.Item(a(i))
Next
toString = s
End Function
Private Sub Exec(a, b, i)
If a <> "" Then
If c_n < c_Max Then
If a <> "を" Then c_n = c_n + 1
c_a.Add i, a
End If
Else
If Instr(b, "</") = 1 Then
If c_n < c_Max Then
c_a.Add i, b
ElseIf c_x = 0 And c_c > 0 Then
c_a.Add i, b
If c_c = 1 Then c_o = False
Else
c_x = c_x - 1
End If
c_c = c_c - 1
Else
If c_n < c_Max Then
c_a.Add i, b
Else
c_x = c_x + 1
End If
c_c = c_c + 1
End If
End If
End Sub
Private Sub Start()
Dim m, i
Call Sd
c_r.Pattern = "(<[^>]+>)|([\S\s])"
c_r.Global = True
Set m = c_r.Execute(c_s)
For i = 0 To m.Count - 1
If c_o = False Then Exit For
Exec m(i).SubMatches(1), m(i).SubMatches(0), i
Next
End Sub
Public Property Get Parse(s, n)
'return String
c_o = True : c_Max = n : c_n = 0 : c_c = 0 : c_x = 0 : c_s = s
c_a.RemoveAll : c_d.RemoveAll
Call Start
Parse = Ss
End Property
End Class
Dim wc, strng : strng = "<font color=""red"" size=""2""><strong><img src=""csdn"" />String</strong>" _
& "<b><img src=""csdn"" />String</b></font><div></div>"
Set wc = new TLeft
With Response
.Write Server.HTMLEncode(wc.Parse(strng, 1))
.Write "<hr />"
.Write Server.HTMLEncode(wc.Parse(strng, 6))
.Write "<hr />"
.Write Server.HTMLEncode(wc.Parse(strng, 7))
End With
Set wc = Nothing
%>
<script type="text/javascript">
var Left = {
Max : 0, //控制查找最大数
o : true, //控制是否继续查找
n : 0, c : 0, x : 0, //n记录字数,c记录未结束标记数, x无效未结束标记数
d : new Array, //d记录所有没有结尾的标记
a : new Array, //a记录所有匹配出的内容
sd : function (a) {
//set d
Left.d[Left.d.length] = a;
return "\xff";
},
ss : function () {
//scan string
var s = Left.a.join(""), a = s.split("\xff"), n = new Array;
for (var i = 0 ; i < a.length ; i ++) {
n[n.length] = a;
if (i < a.length - 1) n[n.length] = Left.d;
}
return n.join("");
},
ex : function (b, c) {
//exec
var a = b || c;
if (c) {
if (Left.n < Left.Max) {
if (a != "\xff") Left.n ++;
Left.a[Left.a.length] = a;
}
} else {
if (/^<\//.test(a)) {
if (Left.n < Left.Max) {
Left.a[Left.a.length] = a;
} else if (Left.x == 0 && Left.c > 0) {
Left.a[Left.a.length] = a;
if (Left.c == 1) Left.o = false;
} else Left.x --;
Left.c --;
} else {
if (Left.n < Left.Max)
Left.a[Left.a.length] = a;
else
Left.x ++;
Left.c ++;
}
}
},
parse : function (s, n) {
//select
var p = /(<[^>]+>)|([\S\s])/g;
Left.n = Left.c = Left.x = Left.d.length = Left.a.length = 0, Left.o = true;
Left.Max = n;
s = s.replace(/<[^>]+\/>/g, Left.sd);
while(Left.o && p.exec(s))
Left.ex(RegExp.$1, RegExp.$2);
//alert(Left.a);
return Left.ss();
}
};
function show(s, n) {
alert(s + " \n\n\n" + Left.parse(s, n));
}
show('<font color="red" size="2"><strong><img src="csdn" \/>String<\/strong><b><img src="csdn" \/>String</b></font><div><\/div>', 1);
show('<font color="red" size="2"><strong><img src="csdn" \/>String<\/strong><b><img src="csdn" \/>String</b></font><div><\/div>', 6);
show('<font color="red" size="2"><strong><img src="csdn" \/>String<\/strong><b><img src="csdn" \/>String</b></font><div><\/div>', 7);
</script>
欢迎光临 黑色海岸线论坛 (http://bbs.thysea.com/) | Powered by Discuz! 7.2 |