Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regular Expression match in Javascript

Posted on 2011-02-14
6
Medium Priority
?
480 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
ID: 34891621
0
 
LVL 5

Expert Comment

by:jijeesh
ID: 34891661
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 1000 total points
ID: 34891670
sandbox\/(.*?)[^\/]+$

Open in new window

0
Technology Partners: 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 75

Expert Comment

by:käµfm³d 👽
ID: 34891782
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
ID: 34891957
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 75

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

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 …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

824 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