Solved

javascript regex to match specific directory construct

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery, dropdown 4 38
Using this to set a value in javascript 1 26
JavaScript error 1 33
addressing a specific html page 9 28
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-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

760 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

14 Experts available now in Live!

Get 1:1 Help Now