1

I have a link

http://www.youtube.com/v/395hgf98f

Link can be posted in another way as well like so

http://youtube.com/v/395hgf98f

These aren't real links I don't think I just typed a bunch of letters. Any ways I want to remove all before the ending pathname.

I tried replace(/^[^/]*//,'');

Which didn't work. And it would be hard since there are 4 backslashes in the url. Could we do something like

v+/ or v/ together to make sure it removes the v and the next backslash together?

Regex is not a strong suit and really only know a bit of it.

EasyBB
  • 6,176
  • 9
  • 47
  • 77
  • 4
    **This might not be a job for regexes, but for existing tools in your language of choice.** What language are you using? You probably don't want to use a regex, but rather an existing module that has already been written, tested, and debugged. If you're using PHP, you want the [`parse_url`](http://php.net/manual/en/function.parse-url.php) function. If you're using Perl, you want the [`URI`](http://search.cpan.org/dist/URI/) module. If you're using Ruby, use the [`URI`](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/uri/rdoc/URI.html) module. – Andy Lester Mar 09 '13 at 20:04
  • I agree with @AndyLester on this. If you **want** to use a regex try and extract the part you want instead of removing the rest: `/(.*?)$` will return just the slug. – Richard Brown Mar 09 '13 at 20:06
  • Hmm I'm sure those are all wonderful languages but I don't have access to advanced languages on the website I host right now.I know dumb right. It's limiting me from learning these all honestly. :( @RichardBrown wait what? That will grab the hrefs ending pathname? – EasyBB Mar 09 '13 at 20:07
  • Tell us more about your situation. What language are you using? – Andy Lester Mar 09 '13 at 20:08
  • I am only using javascript. What I am doing is getting the href in a variable then later I need to use this bit to grab the ending pathname and I'm going to use it for adding an ID to it. – EasyBB Mar 09 '13 at 20:08
  • The `(.*?)` will return a capture group of the end of the string `$` up to the last `/`. – Richard Brown Mar 09 '13 at 20:09
  • Then use existing JavaScript solutions: http://stackoverflow.com/questions/4140324/parse-url-with-javascript http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript – Andy Lester Mar 09 '13 at 20:09
  • so I would do .replace(/(.*?)$/,''); – EasyBB Mar 09 '13 at 20:09
  • @AndyLester not sure that helps me since I believe they are grabbing the address location.pathname.match the urls I have will be in divs called post – EasyBB Mar 09 '13 at 20:11
  • The point is that this is probably a solved problem and you can find tools that will help you better than trying to hash stuff out with regexes. – Andy Lester Mar 09 '13 at 20:15
  • @EasyBB If you really want to use regex, this may solve your problem: `/.*\//`. Examples: `'http://www.youtube.com/v/395hgf98f'.replace(/.*\//, '');` or `'http://youtube.com/v/395hgf98f'.replace(/.*\//, '' );` – rdleal Mar 09 '13 at 21:10

1 Answers1

1

You want everything after the last /, so find the last occurence of that character and get the substring after it:

var u = "http://www.youtube.com/v/395hgf98f";
var lastBit = u.substring(u.lastIndexOf("/")+1);
document.write(lastBit); //outputs 395hgf98f
Andrew Morton
  • 24,203
  • 9
  • 60
  • 84