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.
Related
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,}/
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
I am looping through all the links on a page and matching their href values against the following pattern:
([^/]+)/([0-9]+)/([^/]+)
Problem is there are 2 types of link formats on the page:
1. /video/123/slug
2. /video/123
Number 1. gets captured fine with the above regex but the 2nd fails. I want to make the third piece of the regex (the slug) optional so that both link formats return true when matched agains the regex. How to do this?
Put the last bit in brackets of a non-capturing group and add a ?:
([^/]+)/([0-9]+)(?:/([^/]+))?
Use ? quantifier, which makes your pattern optional. It matches either 0 or 1 occurrence of the pattern.
Also, you need to group the last slash, with your last part of your regex, in a non-capturing group.
([^/]+)/([0-9]+)(?:/([^/]+))?
If you add a ? that should make the last part of the pattern optional.
I am trying to test a string for a state code, the regex I have is
^A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]$
The issue is, if I have something like "CTA12" as a test string, it will get a match of CT. How can I modify my regex to make it only match state codes that are not part of a larger string?
Your use of anchors with alternation is incorrect, ^AB|DC$ means "strings that start with AB or end with DC". To get the ^ and $ to both apply to each element of the alternation, you need to put the alternation in a group, for example ^(AB|DC)$.
Try changing your regex to the following:
^(A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY])$
The alternative to using a group is to put the ^ and $ as a part of each element in the alternation, for example ^AB$|^DC$, but that would make your regex significantly longer so a group is the way to go.