1

I want to use regex to extract the last word from a file path. For example, I have: /xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg

I want to extract the "color" out of the path. The path color have different syntax. The only thing that is consistent is the ending where it is always -color.jpg where color would be any [a-z] word.

Is there an elegant way to do this?

I would really appreciate any help here. Thanks

2
  • I want to clarify that the path is any length, any size, and has one or more hyphens. The only thing consistent is the -color.jpg at the end where "color" is the word I want to extract out. And color is any non-digit word.
    – David
    May 31, 2011 at 3:16
  • Hey people, I know that sometimes using regular expressions isn't the best idea (parsing HTML, etc), but sometimes they simplify a lot the code (like in this case). And sometimes you need them (because of requirements, or because the tool you're using to validate something forces you to enter a regex, etc), so do we have to keep saying Why a regex? or Why not _this_ better than a regex? in cases where it doesn't make much sense? May 31, 2011 at 3:18

5 Answers 5

3

Why could you just take substring rather than using regex?

var path=" /xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg";
var lastHyphen = path.lastIndexOf("-");
var lastDot = path.lastIndexOf(".");
var extractedValue=path.substring(lastHyphen + 1, lastDot);

a more compact version will be

var extractedValue=path.substring(path.lastIndexOf("-") + 1, path.lastIndexOf("."));
1
var matched = /-(\w+).jpg/i.exec('/xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg')[1];
0
0

Why use regex?

var a = '/xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg'
    .split('/').pop()
    .split('-').pop()
    .split('.')[0];

console.log(a);
4
  • hmmm.. to be honest, I think I prefer regex in this case. For a coder who knows regular expressions, reading a code with a simple regex (for this purpose) would be much clean and readable than all those pops and splits. I mean, that's the idea, isn't it? You will always be able to do some tweaks to avoid using Regex ;) May 31, 2011 at 3:14
  • This works! But will this work for a path with any number of "/" or "-" in between?
    – David
    May 31, 2011 at 3:18
  • @oscar: Arguable, I prefer to use regex only when absolutely necessary as it incurs extra overhead (quite a bit more expensive than native string functions). And if you're after readability, split it across multiple lines (no pun intended ;)) and it's pretty easy to follow along. May 31, 2011 at 3:19
  • @david: Yes, it will work as long as you have the format *-color.ext. Any number of / or - prior to that will be ignored. May 31, 2011 at 3:20
0

What about

-(\w+)\.jpg

?

If you don't want to hardcode the extension, you can do:

-(\w+)\.\w+\b

Of course, that will match lots of things, but I'm assuming the text to be matched will be the url ;)

Edit:
It will match two groups, and you need to take only the second one, so just access to the 1st index:

var text = '/xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg';
var pattern = /-(\w+)\.\w+\b/;
var match = pattern.exec(text);
alert(match[1]);  // color

Or do it in one line like @Ryan suggested.

3
  • thx but this didn't work for me. It gave me two strings back "-color.jpg" and "color".
    – David
    May 31, 2011 at 3:20
  • @David What it gave you was two groups: The entire match, and only the color. Let me edit it ;) May 31, 2011 at 3:21
  • Gotcha. Thanks Oscar. I used Ryan's but yours edited one was easier to read for a beginner like me.
    – David
    May 31, 2011 at 3:41
0

Why not just

var path= "/xyz/blahblah/zzz/abc-blah/def-xyz-color.jpg";
/(?:([^-.]+?)\.[^.]+?$)/i.test(path);
var color = RegExp.$1;
alert(color);

Your Answer

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

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