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
Solved

Jscript regular expression for base href

Posted on 2007-11-13
6
342 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add or delete table rows 10 54
CORS Error from Chrome, but FireFox is OK 1 83
How to make a good PHP + MySQL + JS pagination system? 3 33
ASP.NET Content Page 3 29
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

829 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