日期:2014-05-16  浏览次数:20578 次

js正则解析SQL字符串

正则表达式 解析SQL 语法字符串

字符串样例

JScript code


select * from mtab

select * from mtab where id=10

select * (select * from mmtab where id=10) where id=10
//取得 (select * from mmtab where id=10)

select * (select * from mmtab where (id=10 and zz=5)) where id=10
//取得 (select * from mmtab where (id=10 and zz=5))

select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5)
//取得 (select * from mmtab where (id=10 and zz=5))

select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5) order by id desc
//取得 (select * from mmtab where (id=10 and zz=5)) 






所有后边也可能没有where子句

先谢谢大家了







------解决方案--------------------
(?<=select \* )\([^)]+\)
------解决方案--------------------
要写个SQL Parser
可以看一下 TrimQuery http://code.google.com/p/trimpath/wiki/TrimQuery
------解决方案--------------------
(?<=select \* )\([^)]+[)][)]*
------解决方案--------------------
JScript code
var str = "select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5) order by id desc";
    alert(str.match(/\(([^\(]*(\([^\)]*\))*)\)/)[1]);

------解决方案--------------------
一直想学正则,呵呵,帮顶了
------解决方案--------------------
LZ, 今天51回你,该快速结贴给分了,下面正则值多少分就给多少吧......
JScript code

var str = "select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5) order by id desc";
    alert(str.match(/\([^\)]+\)+/g)[0]);

------解决方案--------------------
JScript code
<script>
var arr=["select * from mtab",
         "select * from mtab where id=10",
         "select * (select * from mmtab where id=10) where id=10",
        //取得 (select * from mmtab where id=10)
         "select * (select * from mmtab where (id=10 and zz=5)) where id=10",
        //取得 (select * from mmtab where (id=10 and zz=5))
         "select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5)",
        //取得 (select * from mmtab where (id=10 and zz=5))
         "select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5) order by id desc"]
        //取得 (select * from mmtab where (id=10 and zz=5)) 

var reg=/(^select.*?)(\(?select.*\)?)(where.*?$)/ig;
for(var i=0;i<arr.length;i++)
    document.write(arr[i].replace(reg,"$2")+"<br>");

</script>

------解决方案--------------------
又来看看,还没有得分啊。楼上的朋友,如果字串中没有“select.*”怎么办呢?正则的实质是最大限度抽象和简洁。
JScript code

<script>
var arr=["select * from mtab",
         "select * from mtab where id=10",
         "select * (select * from mmtab where id=10) where id=10",
        //取得 (select * from mmtab where id=10)
         "select * (select * from mmtab where (id=10 and zz=5)) where id=10",
        //取得 (select * from mmtab where (id=10 and zz=5))
         "select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5)",
        //取得 (select * from mmtab where (id=10 and zz=5))
         "select * (select * from mmtab where (id=10 and zz=5)) where (id=10 zz=5) order by 

id desc"]
        //取得 (select * from mmtab where (id=10 and zz=5)) 

var reg=/\([^\)]+\)+/g;
for(var i=0;i<arr.length;i++)
    ! arr[i].match(reg) || document.write(arr[i].match(reg)[0]+"<br>");
</script>