文本:
select num as 序号,code, as 代码, name as 名称
  from (select rownum num, t.*
          from (select t.code code, t.name name
                  from table t                
                 order by t.name desc) t
         where rownum <= #EndPage#)
 where num >= #StartPage#需获取内容:
 num as 序号,code, as 代码, name as 名称
请教:
我只会 (?<=select).*(?=from)   关键就是下面还有一个from 怎么找到第一个from 做符合,像我这句的结果是 
num as 序号,code, as 代码, name as 名称
  from (select rownum num, t.*不符合我想要的

解决方案 »

  1.   

     (?<=select).*?(?=from)
    非贪婪模式试试呢
      

  2.   


    是多行的,
    这样是找出所有的 select 项,我只要找到第一个符合到的select 项,接下来符合就不要了。
      

  3.   

    用非贪婪模式,第一次只能匹配出  num as 序号,code, as 代码, name as 名称
    你就只处理第一次匹配的结果就行了
      

  4.   

    。。这个非贪婪还是不行
    运行结果是select num as 序号,code, as 代码, name as 名称
      from (select rownum num, t.*
              from (select t.code code, t.name name
                      from table t                
                     order by t.name desc) t
             where rownum <= #EndPage#)
     where num >= #StartPage#
      

  5.   


    <script type=text/javascript>
    var str = "select num as 序号,code, as 代码, name as 名称\
        from (select rownum num, t.*\
              from (select t.code code, t.name name\
                     from table t\
                     order by t.name desc) t\
              where rownum <= #EndPage#)\
        where num >= #StartPage#";//第一:
    str.replace(/select\s*(.+?)(?=\r*from)/, function($1, $2){alert($2)});
    //第二:
    alert(/select\s*(.+?)(?=\r*from)/.test(str) ? RegExp.$1 : "");
    //......
    </script>
      

  6.   

    var sql = "...your sql...";
    alert(sql.substring(0, sql.indexOf('from')))
      

  7.   

    再写严谨些:<script type=text/javascript>
    var str = "select num as 序号,code, as 代码, name as 名称\
        from (select rownum num, t.*\
              from (select t.code code, t.name name\
                     from table t\
                     order by t.name desc) t\
              where rownum <= #EndPage#)\
        where num >= #StartPage#";
     
    //第一:
    str.replace(/\bselect\s*(.+?)(?=\r*from\b)/i, function($1, $2){alert($2)});
    //第二:
    alert(/\bselect\s*(.+?)(?=\r*from\b)/i.test(str) ? RegExp.$1 : "");
    //......
    </script>
      

  8.   

    <script type="text/javascript">
    var test="select num as 序号,code, as 代码, name as 名称 from (select rownum num, t.* from (select t.code code, t.name name from table t order by t.name desc) t where rownum <= #EndPage#) where num >= #StartPage#";
    var reg=/select.*?(?=from)/;
    alert(test.match(reg)[0].replace("select",""));
    </script>
    js不支持逆序环视吧?
      

  9.   


    你好,这段我刚刚去JS测试了,是可以了,,不过看不大懂
    有没在这个工具RegExTester.exe 可以直接测试的正则表达式,在这边我理解得会比较懂- -,再帮次啊大侠!!
      

  10.   

    ..我不止前台,后台也要用到正则的,前后台的正则使用方法区别很大。··所以要想句通用正则,就是可以在RegExTester.exe工具上测试的
      

  11.   


    对不起,我没有RegExTester.exe,以前也没用过,正则很难用三言两语准确的说明白,得靠大量练习去融会贯通。
    BTW:很多人为JS正则没有逆向环视所困扰,这也是JS正则应用最大的难点。初开始我也一样,后来发现此类问题同样可以通过正向环视去解决,且JS正则是有能力解决到位的,只是没有逆向环视直观和方便。
      

  12.   

    9楼朋友的代码是最直接的。修正加强一下,你若是想保留num前面的那个空格,就将sql.indexOf('select')+7换成sql.indexOf('select')+6就行了
    字符串的indexOf()方法,总不管是前台还是后台都有吧。
    var sql = "anychar select num as 序号,code, as 代码, name as 名称\n" 
            +"from (select rownum num, t.*" 
            +"from (select t.code code, t.name name" 
            +"from table t" 
            +"order by t.name desc) t" 
            +"where rownum <= #EndPage#)" 
            +"where num >= #StartPage#";
        sql=sql.replace(/(\n|\r|\n\r)/g,'');
        alert(sql.substring(sql.indexOf('select')+7, sql.indexOf('from'))); 
      

  13.   

    str.replace(/\bselect\s*(.+?)(?=\r*from\b)/i, function($1, $2){alert($2)});,不会正则,有没有人给我解释一下啊。多谢多谢。$1 $2是什么意思啊?别见笑啊。
      

  14.   


    indexof 可以。但是,不可能都这么巧可以用到这个,,还是感谢上面各位大侠的热心帮助