• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 44
  • Last Modified:

JS Regex for first level directory with/without Query String?

What would the JS regex be for a directory "/m/" ("m" is the actual directory name) with or without a query string? No file name will ever show, only a possibility of query strings.  Regex has to fail if another directory is present. This is the first level dir on a website which is the value returned by location.pathname. Examples:

Yes - /m/
Yes- /m/?a=b
No - /m/n/
No - /m/n/?a=b
1 Solution
Jan LouwerensSoftware EngineerCommented:
var regex = /^\/m\/(\?.*)?$/;

var tests = ['/m/', '/m/?a=b', '/m/n/', ' /m/n/?a=b'];
for (var index = 0; index < tests.length; index++)
   var test = tests[index];
   console.log('-  %o: %o', test, regex.test(test));

Open in new window

894359Author Commented:
Thank Jan!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now