Solved

Jscript regular expression for base href

Posted on 2007-11-13
6
343 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 250 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Assisted Solution

by:b0lsc0tt
b0lsc0tt earned 250 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

735 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