Solved

Jscript regular expression for base href

Posted on 2007-11-13
6
346 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Node.js 11 77
Angular tyeahead feature not working with Control "as" ControlXXX 4 46
Table style position to the left 8 38
Date Picker is picky (about something) 4 40
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

737 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