13

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=

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.

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

3 Answers 3

23
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);
Sign up to request clarification or add additional context in comments.

8 Comments

hm. did not know that! I always just sort of assumed that since they were so extremely difficult, they had to be good.. I think I will use this if it is indeed faster. Thanks!
([^\?]*)\?order_num=(\d*) should be ([^#?]*)\?order_num=(\d*). Better yet: ([^#?]*)[?&]order_num=(\d*). Even then, that doesn't handle escaped character in order_num or the number. Why doesn't JS have a builtin URL parser?
@thomas Well regexps are good because they can let you do in one line what you usually do in several lines of code. Being so powerful, it means also being slower. Not to mention way harder to maintain... you'll end up rewriting your own regexps instead of fixing them.
@AlanMoore "Popular" isn't the same thing as "good". The best thing is to not make excessive use of them, because there are tasks that are simple enough of good ol' string methods. On the contrary, I think it's a good exercise for the mind to think about an alternative solution before relying on regular expression. Which can be not only slow, but extremely slow if not well-built. Just look at this old but still meaningful page: blog.stevenlevithan.com/archives/faster-trim-javascript See how much performances change?
Actually I do like regexps. A lot. But I always keep in mind all the caveats. And an excellent reference for a good regexp building is regular-expressions.info Very informative indeed, not only for Javascript.
|
10

I see no need for regex for this:

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.

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

3 Comments

Bearing in mind that he wants to get the part after the =...but otherwise, yep.
+1 Then split on & and then on = and you can even handle afdoiajd?iminyourstring=21312&order_num=3216545&throwingyouoff=231221
@DavidThomas Thanks, I've added an example for that.
4

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

1 Comment

+1 Because your answer takes under consideration the possibility that digits might not be at the end of the string. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.