Solved

javascript regex to match specific directory construct

Posted on 2013-05-21
2
265 Views
Last Modified: 2013-05-22
Hello,
I am trying to write a regex expression that returns true or false based on the directory passed in. For example:

var pattern = new RegExp("/^\/application\/modules\/?\/models/i");

console.log(pattern.test("/application/modules/admin/models"));

I've been at this awhile and dont seem to be getting anywhere.. I have tried substituting in * for ? as well as various other constructs. Can someone explain to me what IM missing here?

500 points for working solution.
0
Comment
Question by:richardsimnett
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39186445
var pattern = new RegExp(/^\/application\/modules\/[^\/]*\/models/i);

Open in new window


http://jsfiddle.net/Mzest/
0
 

Author Comment

by:richardsimnett
ID: 39188054
How would I change this to make this work, as I was testing this I realized that there was something I overlooked.

var modulepattern = new RegExp(/^\/application\/modules\/[^\/]*\/models/i);

Example 1
modulepattern.test("/application/modules/test/models"); //returns true as it should - this is perfect

Example 2
modulepattern.test("/application/modules/test/models/test"); //should return false - but currently returns true (note the additional directory after the /models in the path
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…
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…

912 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