regex - get numbers after certain character string

前端 未结 3 1221
执念已碎
执念已碎 2020-12-16 01:24

I have a text string that can be any number of characters that I would like to attach an order number to the end. Then I can pluck off the order number when I need to use it

3条回答
  •  一向
    一向 (楼主)
    2020-12-16 02:13

    You can substring from the first instance of ? onward, and then regex to get rid of most of the complexities in the expression, and improve performance (which is probably negligible anyway and not something to worry about unless you are doing this over thousands of iterations). in addition, this will match order_num= at any point within the querystring, not necessarily just at the very end of the querystring.

    var match = s.substr(s.indexOf('?')).match(/order_num=(\d+)/);
    if (match) {
      alert(match[1]);
    }
    

提交回复
热议问题