Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex strip domain name

A quick simple regex question

I have a domain name in a string that I need to strip - There is always http://www. and the domain always ends in "/"

g_adv_fullpath_old = g_adv_fullpath_old.replace(/http\:\/\/www\.(.*?)\//ig, '');

how do I create the regex to strip the domain name?

Any help would be appreciated

like image 692
Gerald Ferreira Avatar asked Dec 10 '22 10:12

Gerald Ferreira


2 Answers

I would simply split on "/". For example:

>>> "http://www.asdf.com/a/b/c".split("/").slice(3).join("/")
'a/b/c'
like image 111
David Wolever Avatar answered Dec 30 '22 08:12

David Wolever


Why complications? Simple indexOf will do.
First remove http://www (10 characters), then everything before the first slash.

var s = "http://www.google.com/test";
s = s.substr(10);
s = s.substr(s.indexOf('/'));
alert(s);

Or split, as David suggests.

An example

like image 35
Nikita Rybak Avatar answered Dec 30 '22 10:12

Nikita Rybak