Im trying to match a URL's path (window.location.pathname) but exclude anything further down the path.
I want to match the following:
/admin/sites/{2-6 digit number}{/ exclude the rest}
Examples
/admin/sites/123 - true
/admin/sites/1 - false
/admin/sites/123/foo - false
I've got as far as the following regex but can't seem to figure out the rest.
/admin\/sites\/[0-9]/.test(window.location.pathname)
/^\/admin\/sites\/\d{2,6}$/
the $ anchors the expression to the end of the string so it must end with the digits.
I also included the ^ so it must start with /admin.
If you want to match up to the / after the digits, you need the following regex:
^\/admin\/sites\/[0-9]{2,6}(?=\/)
See demo
Related
I'm trying to match the following with this Regexp:
/(?<route>(?:.*\/){1,2}?)(?<group>(?:.*\/){1,2}+)?/
route/group/
route1/route2/group/group2/
route/group1/group2/
route/
Tried to make the first group lazy and the second one greedy and optional but it doesn't match. What am I missing?
Initial Demo: https://regex101.com/r/aRvvQE/2
Assuming there is a file extension at the end of the path then you could use:
(?<route>(?:[^/\s]*\/){1,2}?)(?<group>(?:[^/\s]*\/){0,2})(?=[^/]*\.)
If not then change \. to $ or whatever suits.
I want to match exactly one occurrence of # in a string. I found that /^[^#]+#[^#]+$/ working but not /[^#]+#[^#]+/
Why should I include the search from beginning to end? Wont the pattern anyway check throughout the string? Can someone explain it for me?
Without providing ^ and $, your RegEx will match parts of your string.
Let's demonstrate with some examples :
/^[^#]+#[^#]+$/
matches test#String.
doesn't match test#Str#ing
/[^#]+#[^#]+/
matches test#String
matches the part test#Str of test#Str#ing
Can anyone help me with the REGEX to match
../_assets/applications/cleaning/*logo.png
"*" being the file name which can also follow an underscore or dash so
../_assets/applications/cleaning/main_logo.png
OR
../_assets/applications/cleaning/main-logo.png
this is as far as I got
\assets\/applications\/cleaning\/
An asterisk in a regex is a quantifier allowing zero or more of the previous character/group. So you first expression would allow zero or more forward slashes. You can use a . with a * to allow for zero or more of any character (excluding new line). So something like:
\/cleaning\/(.+?logo\.png)$
should find all the images you want, then:
/logos/$1
should replace them as you wanted.
Demo: https://regex101.com/r/dmAjjv/1/
I need some help with RegEx, it may be a basic stuff but I cannot find a correct way how to do it. Please help!
So, here's my question:
I have a list of URLs, that are invalid because of double slash, like this:
http://website.com//wp-content/folder/file.jpg, to fix it I need to remove all double slashes except the first one followed by colon (http://), so fixed URL is this: http://website.com/wp-content/folder/file.jpg.
I need to do it with RegExp.
Variant 1
url.replace(/\/\//g,'/'); // => http:/website.com/wp-content/folder/file.jpg
will replace all double slashed (//), including the first one, which is not correct.
example here:
https://regex101.com/r/NhCVMz/2
You may use
url = url.replace(/(https?:\/\/)|(\/){2,}/g, "$1$2")
See the regex demo
Note: a ^ anchor at the beginning of the pattern might be used if the strings are entire URLs.
This pattern will match and capture http:// or https:// and will restore it in the resulting string with the $1 backreference and all other cases of 2 or more / will be matched by (\/){2,} and only 1 occurrence will be put back into the resulting string since the capturing group does not include the quantifier.
Find (^|[^:])/{2,}
Replace $1/
delimited: /(^|[^:])\/{2,}/
I would like to have some regular expression to my JS script.
Examples of urls that should not match:
http://www.domain.com/files/pictures/3749832
C://mydocuments/files/pictures/3749832
domain.com:8080/doc/files/pictures/3749832
BUT these should match:
files/pictures/3749832
/files/pictures/3749832
My regex: files/pictures/[0-9]{7} is not enough good :(
You'll need to escape the front slashes in order to get it to work. You'll also want to ensure it matches the start of the string (with or without /) - using the ^ matches the start of line.
^\/?files\/pictures\/\d{7}
Here's a regex101 for you to play around with: https://regex101.com/r/gF5cA0/1
If you need it to also not match anything after this (like a subfolder) use the $ to match the end of line:
^\/?files\/pictures\/\d{7}$