For instance, I am using a simple regex to match everything before the first space:
var str = '14:00:00 GMT';
str.match(/(.*?)\s/)[0]; //Returns '14:00:00 ' note the space at the end
To avoid this I can do this:
str.match(/(.*?)\s/)[0].replace(' ', '');
But is there a better way? Can I just not include the space in the regex? Another examle is finding something between 2 characters. Say I want to find the 00 in the middle of the above string.
str.match(/:(.*?):/)[0]; //Returns :00: but I want 00
str.match(/:(.*?):/)[0].replace(':', ''); //Fixes it, but again...is there a better way?
I think you just need to change the index from 0 to 1 like this:
0 means the whole matched string, and 1 means the first group, which is exactly what you want.
@codaddict give another solution.
(?=\s) means lookahead but not consume whitespace, so the whole matched string is ’14:00:00′ but without whitespace.