search

Home  >  Q&A  >  body text

javascript - How to use regular expressions to match ids after special symbols

str = 'http://1dwen.cn/index.php/bra...';
How to get 2 of this string;

三叔三叔2704 days ago1025

reply all(4)I'll reply

  • 迷茫

    迷茫2017-07-05 10:58:03

    
    function GetQueryString(str, name)
    {
         var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
         var r = str.match(reg);
         if(r!=null)return  unescape(r[2]); return null;
    }

    reply
    0
  • 扔个三星炸死你

    扔个三星炸死你2017-07-05 10:58:03

    http://1dwen.cn/index.php/bra...

    Do I have to use regular expressions? This can also be done using url and querystring.

    var url = require('url'); 
    var qs = require('querystring');
    
    function getQuery(_url_){
        return qs.parse(url.parse(_url_).query); 
    }
    
    var q = getQuery('http://1dwen.cn/index.php/brand/hot_product?id=2'); 
    console.log(q.id); 

    reply
    0
  • 代言

    代言2017-07-05 10:58:03

    var str = 'http://1dwen.cn/index.php/brand/hot_product?id=2';
    str.match(/^(.*)\?id=(\d+)$/)[2]

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:58:03

    Let me write a function that takes parameter values

    var str='http://1dwen.cn/index.php/brand/hot_product?id=2'
    function getParmValue(str){
       if(str.indexOf('?')==-1){
        return false;
    }else{
        var s=str.slice(str.indexOf('?')+1);
        var arr=s.split('=');
        return arr[1];
    } 
    }
    getParmValue(str);

    reply
    0
  • Cancelreply