正则表达式 - 在某些字符串后获取数字 [英] regex - get numbers after certain character string

查看:137
本文介绍了正则表达式 - 在某些字符串后获取数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本字符串,可以是任意数量的字符,我想在最后添加订单号。然后,当我需要再次使用它时,我可以摘下订单号。由于数字可能是可变长度,我想做一个正则表达式,在 = 中签署字符串?order_num后捕获所有内容=

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 again. Since there's a possibility that the number is variable length, I would like to do a regular expression that catch's everything after the = sign in the string ?order_num=

因此整个字符串将是

"aijfoi aodsifj adofija afdoiajd?order_num=3216545"

我试过在线使用正则表达式生成器,但没有运气。有人可以帮我提取最后的数字并将它们放入变量中,然后将?order_num = 203823 之前的数字放入其自己的变量中。

I've tried to use the online regular expression generator but with no luck. Can someone please help me with extracting the number on the end and putting them into a variable and something to put what comes before the ?order_num=203823 into its own variable.

我会发布一些自己的尝试,但我预见到了失败和混乱。

I'll post some attempts of my own, but I foresee failure and confusion.

推荐答案

var s = "aijfoi aodsifj adofija afdoiajd?order_num=3216545";

var m = s.match(/([^\?]*)\?order_num=(\d*)/);
var num = m[2], rest = m[1];

但请记住,正则表达式。尽可能使用 indexOf substring / slice 。例如:

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);

这篇关于正则表达式 - 在某些字符串后获取数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆