views:

58

answers:

5

Hi,

I've got a string like this: 02-09-10, 20:19 (1 dagen geleden). I need the 1 inside the brackets. I used this regex:

myStr.replace(/(.+*)\(([0-9]{1,}) dagen geleden\)/i, '$2')

But that doesn't work... What to do?

Regards, dodo

+1  A: 

The (.+*) is invalid. You want (.+?) instead. And use .match or re.exec to get a substring, not .replace.

If there are multiple appearances:

var rx = /\((\d+) dagen geleden\)/g;
var res = [];
while((m = rx.exec(myStr))) {
  res.push(m[1]);
}
return res;

Otherwise, just use:

var m = myStr.match(/\((\d+) dagen geleden\)/);
if (m)
  return m[1];
else {
  // not found.
}
KennyTM
Also `+` is the same as `{1,}`
adamse
+1  A: 

.+* will consume the whole string as it is greedy, also using both + and * is wrong.

var myStr = "02-09-10, 20:19 (1 dagen geleden)";
myStr.match(/([0-9]+) dagen geleden\)$/)[1]; // 1
adamse
Ok, this works! `myStr.replace(/\d{2}-\d{2}-\d{2}, \d{2}:\d{2} \(([0-9]+) dagen geleden\)$/, "$1");`
dododedodonl
@dodo: It's just not smart to use replace to pluck something out of a string in the manner that you're doing it in - that's exactly what *match()* and *exec()* are provided for. Both KennyTM and adamse have provided suitable, more readable solutions which achieve the same goal.
Andy E
@Andy: Why is it not smart to use replace??
dododedodonl
@dodo: It's not smart because it doesn't make any sense to use it that way. There are times when a replace can be useful for parsing a string (and not actually replacing anything), but your regex is so explicit that you might as well use *substring()* and *indexOf()* instead. Like I said, take a look at the answers from KennyTM and adamse, they do the same think but in a more correct way.
Andy E
A: 

To find it

var a = "02-09-10, 20:19 (1 dagen geleden)".match(/\(([0-9]+)/)

To get the number

parseInt( a[1], 10)

If you want to replace 1 by 2

"02-09-10, 20:19 (1 dagen geleden)".replace(/\(([0-9]+)/, '(2')

And... 1 dagen geleden is wrong grammatically ;)

Mic
A: 

Here's your first regex attempt:

% js
Rhino 1.7 release 2 2009 03 22
js> var x = '02-09-10, 20:19 (1 dagen geleden)';
js> x.replace(/(.+*)\(([0-9]{1,}) dagen geleden\)/g, '$2');
js: uncaught JavaScript runtime exception: SyntaxError: Invalid quantifier *

As someone already mentioned, you don't want that asterisk. Just get rid of it:

js> x.replace(/(.+)\(([0-9]{1,}) dagen geleden\)/g, '$2');
1

There's the "1" you wanted.

But you can make this better. Why say '[0-9]{1,}' when '\d+' means the same thing? Also, are you sure there will never be characters beyond that final ")"? Will there ever be stray spaces, or could the spaces be other kinds of whitespace (a tab, maybe)? If you really just want that number before "dagen geleden" I'd write it like this:

js> x =  x.match(/\((\d+)\s+dagen\s+geleden\)/)[1];
1

It will catch that number it even if your string has unexpected garbage:

js> "garbage (255 dagen geleden) more garbage".match(/\((\d+)\s+dagen\s+geleden\)/)[1];
255
drench
The string will always be in the format I said... I'll try the match thing!!
dododedodonl
A: 

I'm not quite sure how I found myself back at this question, but I decided to expand on a point I made in the comments earlier. You're using string.replace() where string.match() or regex.exec() would be more appropriate.

Furthermore, you can achieve your goal without even using regular expressions, which should be more efficient and more understandable if you're not too savvy on regexes:

// The input string:
var myStr = "02-09-10, 20:19 (1 dagen geleden)";

// substring() would work just as well in place of slice() here:
alert(myStr.slice(
    // slice from the 1st character after the opening brace,
    myStr.indexOf("(")+1,
    // to the space just before "dagen"
    myStr.indexOf(" dagen")
));
// -> 1

In fact, the regular expression you came up with is so explicit, you could change the first argument of slice to a number you know:

myStr.slice(17, myStr.indexOf(" dagen"));
// -> 1
Andy E
Thanks!! Didn't think of this!! It is indeed always in the format that I said, so, this is faster!
dododedodonl