Regex That Wil Match After Keyword And Before Question Mark
Solution 1:
Try capturing your values in a capturing group:
Explanation
recepten-zoeken
Match literally\/
Match forward slash([^?]+)
Capture in a group a negated character class[^
which will match NOT a question mark. (This will contain your value)\?
Match literally
var s = "https://localhost:8443/site/recipes/recepten-zoeken/menugang:hoofdgerecht/soort:italiaans/seizoen:winter?Nrpp=24"console.log(s.match(/recepten-zoeken\/([^?]+)\?/)[1])
Credits to ctwheels for providing the snippet and comment.
Solution 2:
Use capturing group.(()
brackets indicates capturing group)
Capture upto last question mark
Try this regex \/recepten-zoeken\/(.*)\?
Explanation:-
\/
Match forward slashrecepten-zoeken
Match literally\/
Match forward slash(.*)
Capture in a group all value(except new line) upto last question mark in string(This will contain your value)\?
Match literally
//-------------------------select upto last ? -----------------------------
str = "https://localhost:8443/site/recipes/recepten-zoeken/menugang:hoofdgerecht/soort:italiaans/seizoen:winter?Nrpp=24";
var myRegexp = /\/recepten-zoeken\/(.*)\?/;
console.log(myRegexp.exec(str)[1]);
Capture upto first question mark
Try this regex \/recepten-zoeken\/([^?]*)\?
.
Explanation:-
\/
Match forward slashrecepten-zoeken
Match literally\/
Match forward slash([^?]*)
Capture in a group all value(except ?) upto first question mark in string(This will contain your value)(here [^?] means match any character except?
)\?
Match literally
//-------------------------select upto first ? -----------------------------var str = "https://localhost:8443/site/recipes/recepten-zoeken/menugang:hoofdgerecht/soort:italiaans/seizoen?:winter?Nrpp=24";
var myRegexp = /\/recepten-zoeken\/([^?]*)\?/;
console.log(myRegexp.exec(str)[1]);
Solution 3:
This would do it:
var match = url.match(/\/recepten-zoeken\/([^?]+)\?/);
// match[1] == "menugang:hoofdgerecht/soort:italiaans/seizoen:winter"
This regex will match the first occurrence of /recepten-zoeken/
, then it will start capturing all characters that are not question marks ([^?]
is a negative character class which matches anything not in the class).
Note that it will also ensure that there is a question mark. If you want to support cases where there is no question mark, then just remove the final \?
.
Your original regular expression only matches a single character (the .
), then it tries to look ahead for characters that are not c
, e
, k
, /
or between n
and z
.
Post a Comment for "Regex That Wil Match After Keyword And Before Question Mark"