Reputation: 1798
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
Reputation: 1798
Got it! I used this:
href = href.match("(http:\/\/.+.com)")[0];
Upvotes: 0
Reputation: 5207
/https?:\/\/[^/]*/.exec('http://www.google.com/search-page.html')
output:
Upvotes: 1
Reputation: 707308
I think you can use this:
var firstPart = href.match(/^(.+?:\/\/.+?)\//)[1];
Working demo: http://jsfiddle.net/jfriend00/xJkLD/
Upvotes: 1