Solved

javascript regex to match specific direcotry

Posted on 2013-05-22
2
259 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Clear input text 15 27
Debugging Html 8 32
Github api to fetch the number of collaborators in a git repo 1 31
Why is my $_POST not going to results page 10 41
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

733 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