regex - get numbers after certain character string

前端 未结 3 1223
执念已碎
执念已碎 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:21

    var s = "aijfoi aodsifj adofija afdoiajd?order_num=3216545";
    
    var m = s.match(/([^\?]*)\?order_num=(\d*)/);
    var num = m[2], rest = m[1];
    

    But remember that regular expressions are slow. Use indexOf and substring/slice when you can. For example:

    var p = s.indexOf("?");
    var num = s.substring(p + "?order_num=".length), rest = s.substring(0, p);
    

提交回复
热议问题