2

I just can't get this thing to work in javascript. So, I have a text "game_1" without the quotes and now i want to get that number out of it and I tried this:

var idText = "game_1";
re = /game_(.*?)/;
found = idText.match(re);

var ajdi = found[1];
alert( ajdi );

But it doesn't work - please point out where am I going wrong.

1
  • Is this always the whole text, "game_X" ? I mean, does the string only consist of this? Jul 7, 2011 at 9:10

6 Answers 6

5

If you're only matching a number, you may want to try

/game_([0-9]+)/

as your regular expression. That will match at least one number, which seems to be what you need. You entered a regexp that allows for 0 characters (*) and let it select the shortest possible result (?), which may be a problem (and match you 0 characters), depending on the regex engine.

9
  • I tried your test here: regular-expressions.info/javascriptexample.html but it's not working :(
    – Nikola
    Jul 7, 2011 at 9:13
  • [0-9]+ is more precise, which is always better.
    – katspaugh
    Jul 7, 2011 at 9:14
  • Really? It works perfectly for me both on that page and on Regexp Buddy. Did you remove the // before trying it on the page?
    – Naltharial
    Jul 7, 2011 at 9:17
  • @Nikola: Please provide an example where this expression does not work. It should! Jul 7, 2011 at 9:21
  • 1
    The site you're testing on requires you to remove the slashes. I added the slashes because you use them in javascript. Try testing with game_([0-9]+).
    – Naltharial
    Jul 7, 2011 at 9:29
4

If this is the complete text, then there is no need for regular expressions:

var id = +str.split('_')[1];

or

var id = +str.replace('game_', '');

(unary + is to convert the string to a number)


If you insist on regular expression, you have to anchor the expression:

/^game_(.*?)$/

or make the * greedy by omitting the ?:

/game_(.*)/

Better is to make the expression more restrictive as @Naltharial suggested.

1
  • nice, good one! But since I asked for a regex, I'll mark katspaugh as correct answer.
    – Nikola
    Jul 7, 2011 at 9:15
1

Simple string manipulation:

var idText = "game_1",
    adji = parseInt(idText.substring(5), 10);
1

* means zero or more occurrences. It seems that combining it with a greediness controller ? results in zero match.

You could replace * with + (which means one or more occurrences), but as @Felix Kling notes, it would only match one digit.

Better to ditch the ? completely.

http://jsfiddle.net/G8Qt7/2/

2
  • 1
    Or add anchors: re = /^game_(.*?)$/;
    – jensgram
    Jul 7, 2011 at 9:11
  • @Felix Kling, ? to blame, that Ebenezer Scrooge. Thanks!
    – katspaugh
    Jul 7, 2011 at 9:24
0

Try "game_1".replace(/^(game_)/, '')
this will return the number

0

You can simply use this re /\d+/ to get any number inside your string

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.