Solved

Regular Expression match in Javascript

Posted on 2011-02-14
6
464 Views
Last Modified: 2012-05-11
Can you please let me know how I can express the following line using regular expressions in JavaScript?

http://www-internal.domain.com/support/devel/sandbox/anystring/anystring/anystring/anystring/anystring.html

The "anystring" can be anything, either a letter or a number and I don't know how many times it will repeat itself between slashes.

I want to match the part using regular expressions in JavaScript to the end of the directory where anystring.html HTML file locates.

Example:

http://www-internal.domain.com/support/devel/sandbox/tolgar/Myglnxa64_g934414_mytest/mybugs/mygecks/mychecks.html


Now I want to match: http://www-internal.domain.com/support/devel/sandbox/tolgar/Myglnxa64_g934414_mytest/mybugs/mygecks/

Again, I don't know how many sub directories there are until mychecks.html file.


Thanks,

Tolgar
0
Comment
Question by:Tolgar
6 Comments
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
0
 
LVL 5

Expert Comment

by:jijeesh
Comment Utility
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 250 total points
Comment Utility
sandbox\/(.*?)[^\/]+$

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Another option:
var input = "http://www-internal.domain.com/support/devel/sandbox/tolgar/Myglnxa64_g934414_mytest/mybugs/mygecks/mychecks.html";

alert(input.match(/sandbox\/(.+?)\.html/)[1]);

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
re kaufmed's solution, I don't think we're supposed to include the filename part of the file, which I believe your pattern does.  You are excluding the filetype but not the filename.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
Indeed. I read the example's output incorrectly. Good eye  = )
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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)

771 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

12 Experts available now in Live!

Get 1:1 Help Now