2

I have a URL which may be formatted like this: http://domain.com/space/all/all/FarmAnimals

or like this: http://domain.com/space/all/all/FarmAnimals?param=2

What regular expression can I use to return the expression FarmAnimals in both instances?

I am trying this:

var myRegexp = /\.com\/space\/[a-zA-Z0-9]*\/[a-zA-Z0-9]*\/(.*)/;
var match = myRegexp.exec(topURL);
var full = match[1];

but this only works in the first instance, can someone please provide an example of how to set up this regex with an optional question mark closure?

Thank you very much!

5 Answers 5

5
 /[^/?]+(?=\?|$)/

Any non-/ followed by either ? or and end-of-line.

Sign up to request clarification or add additional context in comments.

6 Comments

returns FarmAnimals?param=2 on http://domain.com/space/all/all/FarmAnimals?param=2
I think [^/]+ needs to be reluctant. That is: [^/]+?
Hah, that works too! Move the question mark to a different spot, where it means something entirely different, and still fixes the problem.
@Wrikken why seems wrong ? working great ! - what is the ?= sign means ? look ahead ?
Well, what I dislike about it is that "param=2" also matches (run with /g), but it would always be the second match, so no biggie. And yup, lookahead it is. I'm not used to regexes without look-behinds (which javascript doesn't support), maybe that's my unease, cause I would have thrown in a (?<=/) for good measure...
|
2

I wouldn't write my own regex here and let the Path class handle it (if those are your two string formats).

string url = "http://domain.com/space/all/all/FarmAnimals";

//ensure the last character is not a '/' otherwise `GetFileName` will be empty
if (url.Last() == '/') url = url.Remove(url.Length - 1);

//get the filename (anything from FarmAnimals onwards)
string parsed = Path.GetFileName(url);

//if there's a '?' then only get the string up to the '?' character
if (parsed.IndexOf('?') != -1) 
    parsed = parsed.Split('?')[0];

Comments

1

You could use something like this:

var splitBySlash = topURL.split('/')
var splitByQ = splitBySlash[splitBySlash.length - 1].split('?')
alert(splitByQ[0])

Explanation:

splitBySlash will be ['http:','','domain.com', ... ,'all','FarmAnimals?param=2'].

Then splitByQ will grab the last item in that array and split it by ?, which becomes ['FarmAnimas','param=2'].

Then just grab the first element in that.

Comments

1

This

.*\/(.*?)(\?.*)?$

Should capture the part of the string you are looking for as the group 1 (and the query after ? in group 2, if needed).

Comments

0
var url = 'http://domain.com/space/all/all/FarmAnimals?param=2';
//var url = 'http://domain.com/space/all/all/FarmAnimals';
var index_a = url.lastIndexOf('/');
var index_b = url.lastIndexOf('?');
console.log(url.substring(index_a + 1, (index_b != -1 ? index_b : url.length)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.