0

How can I extract a string and store it as a variable? I have this URL:

http://sub.site.com/WordsHere-t.jpg

I want to be able to able to grab just WordsHere. It can be any length, it will not always be 9 characters. What's the best way to detect the words after the / and before the -?

var url = "http://sub.site.com/WordsHere-t.jpg";
var getWords = insertRegexHere(url);
document.write(getWords);

This should return the string "WordsHere"

O P
  • 1,997
  • 9
  • 33
  • 66

4 Answers4

4

This will work...

var url = "http://sub.site.com/WordsHere-t.jpg";
var getWords = url.match(/.*\/(.*)-/)[1];

The regex breaks down like this...

  • .*\/ find everything up to the last single slash
  • (.*)- CAPTURE everything up to the dash

Here's a fiddle: FIDDLE DEMO

And a rubular to show how the Regex works: RUBULAR DEMO

Edit - and the good thing is it will also work for URLs like this, as the

http://sub.site.com/sub1/sub2/WordsHere-t.jpg

Edit2 - I shortened the RegEx... I think everyone is overthinking this one... it's not necessary to test for the double slashes at the beginning. Only to find the last slash.

Charlie74
  • 2,791
  • 12
  • 22
1

This is an alternate way, kind of silly, if you don't wanna use regex:

var getWords = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('-'));

This will get you everything between the last / and the last - , but it's dependent on the url structure. If it's always gonna be something like you showed, then you should be just fine, otherwise regex might be a better solution as others suggested.

G4bri3l
  • 3,855
  • 3
  • 26
  • 47
  • Actually, it's not silly. My only doubt is about cases when there are no hyphens in that URL (which my regex kind of covers), but that's easily adjustable. ) – raina77ow Apr 10 '14 at 21:54
  • OP said he wants words between `/` and `-` – Ani Apr 10 '14 at 21:55
  • @raina77ow Yeah, exactly! It's a starting point I think and depends on what kind of urls he/she has to check. – G4bri3l Apr 10 '14 at 21:57
  • @Ani Exactly what this line of code does, or am I mssing something ? – G4bri3l Apr 10 '14 at 21:58
  • @G4bri3l you are right on this. You were wrong on your comment. `window.location.pathname` gives only the path after the domain. – Ani Apr 10 '14 at 22:02
  • @Ani you're 100% right! I didn't use window.location.pathname to test your solution, my bad ;) . – G4bri3l Apr 10 '14 at 22:05
0

Try this: Fiddle - http://jsfiddle.net/Anee/Vtm77/

var path = window.location.pathname; // Get's the path after host url
var arrypath = path.split("-"); // Split's your substring using "-",
console.log(arrypath[0]); // Your expected string
Ani
  • 4,241
  • 4
  • 23
  • 29
  • You should read up on [this](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice), and `try this` is not an explanation of your answer. – Sterling Archer Apr 10 '14 at 21:51
  • 1
    Relax, I'm assuming he's only using `document.write` to display the result for debugging purposes. – kei Apr 10 '14 at 21:53
  • That's what I thought :P – Ani Apr 10 '14 at 21:54
  • 1
    Anyway this doesn't solve the problem, it splits the string in two substrings, first half is everything before "-" and second half is everything after "-", so what you're returning here is http://sub.site.com/WordsHere, which is not what he asked for. He wants just "WordsHere", so this solution might require some extra work to solve the problem. – G4bri3l Apr 10 '14 at 21:55
  • @kei that's... that's worse than using `alert` for debugging. =x – Sterling Archer Apr 10 '14 at 21:56
  • 1
    `document.write` is terrible for debugging - removing it changes the way your app works, defeating the purpose. Use `console.log` instead. – SomeKittens Apr 10 '14 at 21:58
  • 1
    If the URL is `sub.site.com/WordsHere`, then `location.pathname` by itself would return `/WordsHere`. The only issue really with this solution is if the pathname was longer (e.g. `/sub1/sub2/sub3/WordsHere-t.jpg`) then it would return the entire path and not just the text after the last `/` – kei Apr 10 '14 at 21:59
  • He can fix that using "arrypath[0].substring(arrypath[0].lastIndexOf('/')+1)" and it will always return the exact word! – G4bri3l Apr 10 '14 at 22:12
0

The regex you want is probably "^[^/]//[^/]/([^-.]*)-" and the first capture will be the word you want.

Jon Watte
  • 5,534
  • 4
  • 40
  • 51