Solved

javascript regex to match specific direcotry

Posted on 2013-05-22
2
253 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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 …
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

18 Experts available now in Live!

Get 1:1 Help Now