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
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
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.
}
.+*
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
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 ;)
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
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