一个正则表达式的算法问题
有两个字符串
str1= "ABCDEF "
str2= "AD "
如何通过正则表达式比较两个字符串,得出str1是否包含str2所有字符,如果包含返回true,不完全包含例如str2= "AGZX "则返回False
------解决方案-------------------- <script type= "text/javascript ">
var s = "ABCDEFAD ", b = "AD ", a = [], i;
var new_a = [], n;
for (i = b.length ; i > 0 ; i --) {
a[a.length] = b.substring(0, i);
}
for (i = 0 ; i < a.length ; i ++) {
n = new_a[new_a.length] = {};
n.length = 0;
n.string = s.replace(new RegExp(a[i].replace(/\^\\\[\]\(\)\$/g, function (a) {
return "\\ " + a;
}), "g "), function (a) {
n.length ++;
return " <font color=\ "#FF0000\ "> " + a + " <\/font> ";
})
};
for (i = 0 ; i < new_a.length ; i ++) {
n = new_a[i];
document.write(n.string + " " + n.length + " <br \/> ");
}
</script>
------解决方案-------------------- <script type= "text/javascript ">
var s = new Array(200000).join( "ABCDEFAD "), b = "AD ", a = [], i;
var new_a = [], n, st = new Date;
for (i = b.length ; i > 0 ; i --) {
a[a.length] = b.substring(0, i);
}
for (i = 0 ; i < a.length ; i ++) {
n = new_a[new_a.length] = {};
n.length = s.match(new RegExp(a[i].replace(/\^\\\[\]\(\)\$/g, function (a) {
return "\\ " + a;
}), "g ")).length;
n.string = a[i];
};
new_a.sort(function (a, b) {
var a = a.length, b = b.length;
return a > b ? -1 : 1;
});
alert( "所用时间: " + (new Date - st));
for (i = 0 ; i < new_a.length ; i ++) {
n = new_a[i];
alert(n.string + ": " + n.length);
//document.write(n.string + " " + n.length + " <br \/> ");
}
</script>
------解决方案--------------------学习,收藏!
------解决方案--------------------我虽然具体的不太清楚
不过你可以参考一下
http://www.pigcode.com/dispbbs.asp?boardid=2&id=205