Reputation: 1845
I have Strings in this format:
6b255c1c689e2e7ee88e6d5d8ab10e9b_307_17234.jpg
8b342878cb8d848608d422a2c02b5e32_7_240723.jpg
07e8e2376d12e2d20a1e4c2159569c43_243_141599.jpg
How do you get the last 2 sets of Numbers?
For example, in
07e8e2376d12e2d20a1e4c2159569c43_243_141599.jpg
I want to get:
07e8e2376d12e2d20a1e4c2159569c43_243_141599.jpg Only the Numbers in bold with the underscore in between them
Is it possible??.....
I tried: var getDigts = str.split('_'), //this way is ok but I have to split again the getDigts[2] as in str2.split(".") to get rid of the DOT.
Is there a workaround or something quicker than double split
with plain REGEX
?
Thx
EDIT
I tried to use str.match(/(\d+_\d+).jpg/)[1]
as suggested by @Michael Best ... Just experienced a big ERROR: The reason is because am looping through each string to get the numbers... NOW, Should there be something like:
6b255c1c689e2e7ee88e6d5d8ab10e9b_307_17234.jpg 8b342878cb8d848608d422a2c02b5e32_7_240723.jpg 649ff5658ad274d8cee67b1f6d34600c_90_182456.png 07e8e2376d12e2d20a1e4c2159569c43_243_141599.jpg
Notice the: .png
line. is there a REGEX to match both .jpg
and/or .png
at the same time?
I tried to remove the jpg
from the .jpg
something like: str.match(/(\d+_\d+)./)[1]
but this fails when it comes to strings like: 07e8e2376d12e2d20a1e4c2159569c43_243_141599.jpg
it returns
07e8e2376d12e2d20a1e4c 2159569c43_243 _141599.jpg
instead of
07e8e2376d12e2d20a1e4c2159569c43_243_141599.jpg
Any Help is highly appreciated...
Upvotes: 1
Views: 112
Reputation: 16688
Here's a quick-and-dirty regex:
var nums = str.match(/(\d+_\d+)\.\w+$/)[1];
Upvotes: 2
Reputation: 4692
You can try:
str.match(/(\d+_\d+).JPG|PNG/i)[1]
When you say: .JPG|.PNG
and you add the i
athe end, you are telling the script to look for those extensions in a case insensitive way.
So it will match both .jpg
and .png
Hope it helps
Upvotes: 2
Reputation: 9131
You should try this regexp.
str.match(/(\d+_\d+)\.(?:jpg|png)/i)[1]
The .
have to be escaped as well. .
is a wildcard for nearly all characters. If not escaped then
1233242_34243_3224_png_3424_34342.jpg
Whould give a wrong match 34243_3224
.
Because of using case insensitivity with i
-flag PNG and png (JPG, jpg) would be matched as extensions.
Upvotes: 2