Darth Coder
Darth Coder

Reputation: 1798

RegEx for omitting part of URL to ahead of domain name

I need to do the following n javascript using regex and split function:

input: http://www.google.com/some-page.html output: http://www.google.com

I tried the following, but it doesnt work, it just returns "http://www.google."

href = href.split("com/")[0];

I am sure I must have made some noob mistake but sorry I am new to this! Thanks!

Upvotes: 0

Views: 43

Answers (3)

Darth Coder
Darth Coder

Reputation: 1798

Got it! I used this:

 href = href.match("(http:\/\/.+.com)")[0];

Upvotes: 0

xiaowl
xiaowl

Reputation: 5207

/https?:\/\/[^/]*/.exec('http://www.google.com/search-page.html')

output:

["http://www.google.com"]

Upvotes: 1

jfriend00
jfriend00

Reputation: 707308

I think you can use this:

var firstPart = href.match(/^(.+?:\/\/.+?)\//)[1];

Working demo: http://jsfiddle.net/jfriend00/xJkLD/

Upvotes: 1

Related Questions