Solved

javascript regex to match specific directory construct

Posted on 2013-05-21
2
268 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

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
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…

839 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