?
Solved

Jscript regular expression for base href

Posted on 2007-11-13
6
Medium Priority
?
351 Views
Last Modified: 2012-06-27
Hi X-perts,

I need to fix the following code to return only a base href as "http://www.mydomain.com/" or "http://mydomain.com" WITHOUT any dirs

<script type="text/javascript">
      //var base = document.baseURI;
      var base = document.location.href;
      if (base && base.match(/(.*)\/([^\/]+)/)) {
            base = RegExp.$1 + "/";
      }
</script>

it works fine, if href is something like http://www.mydimain.com/test.html, but returns all the subdirs in case href://www.mydomain.com/dir1/dir2 etc.

How should i change the reg expression to cut off the subdirs?

Thank you

A
0
Comment
Question by:andy7789
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20275907
andy7789,

Use ...

base.match(/http:\/\/[^\/]+\/?/)

Let me know if you have any questions or need more information.

b0lsc0tt
0
 
LVL 63

Accepted Solution

by:
Zvonko earned 1000 total points
ID: 20275945
Check this:

<script type="text/javascript">
      var base = "http://"+window.location.host;

</script>



0
 

Author Comment

by:andy7789
ID: 20275968
no it does not work... it returns

 if (base && base.match(/http:\/\/[^\/]+\/?/)) {
base = RegExp.$1 + "/";
 }

it returns base as a simple /
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 1000 total points
ID: 20276018
Zvonko's solution is probably better anyways, if you don't mind giving up the expression.   To make my solution work you would either add parentheses (to create the capturing group) or just get the match itself.  I did not see that you needed a group so I left it out (sorry).  I suggest not using the group at all.

var base = document.location.href;
base = base.match(/http:\/\/[^\/]+\/?/);
</script>

You don't need the if or to use the group.  Let me know if you have a question or need more info.  That expression will work (and you did want one) but Zvonko's solution is good too. :)

bol
0
 

Author Comment

by:andy7789
ID: 20276195
Thanks guys!. Both the solutions work perfectly.. just tested on the major browsers. Probably, zvonko's code is simpler (I always hate reg expressions, because I do not know them well :)..)
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 20276274
Your welcome!  I'm glad I could help.  I love regex so when I see it asked for in a question I often overlook other options.  I'm glad Zvonko posted about host so you at least have the option and know about it.

Thanks for the grade, the points and the fun question.

bol
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question