var s = "index2.php?option=content&task=showcategory&fun=show";
   function getValue(key){
       var a = s.split(/\?|=|&/);
       for(var i=0,len=a.length;i<len;i++)if(a[i]==key)return a[i+1];
       return null;
   }
   alert(getValue('task'));

解决方案 »

  1.   


       var s = "index2.php?option=content&task=showcategory&fun=show";
       function getValue(key){
    return s.match(new RegExp(key+'=(\\w+)&'))[1];
       }
       alert(getValue('task'));
      

  2.   


      var text = 'index2.php?option=content&task=showcategory&fun=show';
      var reg = /& *task *=.*&/ig;
      var result = text.match(reg)[0];
      result = result.slice(result.indexOf('=') + 1, -1);//result即為樓主要的值了
      

  3.   

    index2.php?option=content&task=showcategory&fun=show是个变量怎么就报错?
      

  4.   

    没问题啊。<script language=javascript>
       function getValue(key,str){
        return str.match(new RegExp(key+'=(\\w+)&'))[1];
       }
    </script>
    <input id='content' type="text" value="index2.php?option=content&task=showcategory&fun=show" />
    <input type="button" value='点点看' onclick="alert(getValue('task',document.getElementById('content' ).value))"/>
      

  5.   

    index2.php?option=content&task=showcategory和index2.php?option=content&task=showcategory&fun=show
    这样能都获取到
      

  6.   

    2楼那个肯定没问题。另一个要用的话,可以这么改。   function getValue(key,str){
        return str.match(new RegExp(key+'=(\\w+)&?'))[1];
       }推荐用2楼的。
       function getValue(key,str){
           var a = str.split(/\?|=|&/);
           for(var i=0,len=a.length;i<len;i++)if(a[i]==key)return a[i+1];
           return null;
       }
      

  7.   

    如果不用2楼的,另一个加点可靠性保障。function getValue(key,str){
      try{
        return str.match(new RegExp(key+'=(\\w+)&?'))[1];
      }catch(e){
        alert('no value of '+key);
        return null;
      }
    }
      

  8.   

    干脆测试代码也给你:<script language=javascript>
       function getValue(key,str){
    try{    return str.match(new RegExp(key+'=(\\w+)&?'))[1];}catch(e){alert('no value of '+key);return null}
       }
       function get(o){
           alert(getValue(o.value,document.getElementById('content' ).value));  
       }
    </script>
    <input id='content' type="text" value="index2.php?option=content&task=showcategory&fun=show" />
    <input type="button" value='option' onclick="get(this)"/>
    <input type="button" value='task' onclick="get(this)"/>
    <input type="button" value='fun' onclick="get(this)"/>
    <input type="button" value='error' onclick="get(this)"/>