Solved

Jscript regular expression for base href

Posted on 2007-11-13
6
332 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now