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=
我有一个文本字符串,可以是任意数量的字符,我想要将一个订单号附加到末尾。然后,当我需要再次使用订单号时,我可以提取订单号。由于数字可能是可变长度的,所以我想做一个正则表达式,在字符串?order_num=中的=符号之后捕获所有的内容
So the whole string would be
所以整个弦
"aijfoi aodsifj adofija afdoiajd?order_num=3216545"
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.
我试过使用在线正则表达式生成器,但没有成功。有人能帮我把末尾的数字提取出来放到一个变量中吗?order_num=203823之前的东西放到它自己的变量中。
I'll post some attempts of my own, but I foresee failure and confusion.
我将发表一些我自己的尝试,但我预见失败和混乱。
3 个解决方案
#1
19
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:
但是请记住,正则表达式是缓慢的。尽可能使用indexOf和substring/slice。例如:
var p = s.indexOf("?");
var num = s.substring(p + "?order_num=".length), rest = s.substring(0, p);
#2
10
I see no need for regex for this:
我认为不需要regex:
var str="aijfoi aodsifj adofija afdoiajd?order_num=3216545";
var n=str.split("?");
n
will then be an array, where index 0 is before the ? and index 1 is after.
n将是一个数组,其中索引0在?而索引1在后面。
Another example:
另一个例子:
var str="aijfoi aodsifj adofija afdoiajd?order_num=3216545";
var n=str.split("?order_num=");
Will give you the result: n[0]
= aijfoi aodsifj adofija afdoiajd
and n[1]
= 3216545
会得到结果:n[0] = aijfoi aodsifj adofija afdoiajd n[1] = 3216545
#3
2
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.
您可以从第一个实例?然后,regex将消除表达式中的大部分复杂性,并改进性能(无论如何,这可能是可以忽略的,也不需要担心,除非您正在进行数千次迭代)。此外,这将在querystring中的任何点匹配order_num=,而不只是在querystring的末尾。
var match = s.substr(s.indexOf('?')).match(/order_num=(\d+)/);
if (match) {
alert(match[1]);
}
#1
19
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:
但是请记住,正则表达式是缓慢的。尽可能使用indexOf和substring/slice。例如:
var p = s.indexOf("?");
var num = s.substring(p + "?order_num=".length), rest = s.substring(0, p);
#2
10
I see no need for regex for this:
我认为不需要regex:
var str="aijfoi aodsifj adofija afdoiajd?order_num=3216545";
var n=str.split("?");
n
will then be an array, where index 0 is before the ? and index 1 is after.
n将是一个数组,其中索引0在?而索引1在后面。
Another example:
另一个例子:
var str="aijfoi aodsifj adofija afdoiajd?order_num=3216545";
var n=str.split("?order_num=");
Will give you the result: n[0]
= aijfoi aodsifj adofija afdoiajd
and n[1]
= 3216545
会得到结果:n[0] = aijfoi aodsifj adofija afdoiajd n[1] = 3216545
#3
2
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.
您可以从第一个实例?然后,regex将消除表达式中的大部分复杂性,并改进性能(无论如何,这可能是可以忽略的,也不需要担心,除非您正在进行数千次迭代)。此外,这将在querystring中的任何点匹配order_num=,而不只是在querystring的末尾。
var match = s.substr(s.indexOf('?')).match(/order_num=(\d+)/);
if (match) {
alert(match[1]);
}