Solved

javascript regex to match specific direcotry

Posted on 2013-05-22
2
255 Views
Last Modified: 2013-05-22
Hello,
I have the following regex (provided to me from another question I asked). 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

I need to know what to add to the regex pattern to make both examples produce the desired results.

Worth 500 points.
0
Comment
Question by:richardsimnett
2 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 39189104
Try this:
var modulepattern = new RegExp(/^\/application\/modules\/[^\/]*\/models\/?$/i); 

Open in new window


Tested here: http://jsfiddle.net/wmM4H/

Note that I've allowed there to be an optional / on the end. If you don't want to allow that, then use:

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

Open in new window

0
 

Author Closing Comment

by:richardsimnett
ID: 39189204
Perfect. Thanks so much!
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

Title # Comments Views Activity
jQuery Parallax 1 38
Download a website to hdd 2 48
Split in Javascript 5 30
jQuery - how can I set focus to first valid element in dropdown? 2 13
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

932 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

11 Experts available now in Live!

Get 1:1 Help Now