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}$
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,}/
I need a regex expression that will match either a url pattern or a given string [MY_NAME]
Separately both of them are pretty straight forward:
/^(http|https):\/\/[^ "]+$/
/\[MY_NAME\]/
but I cannot combine them together in a single regex expression
You need to use a pipe |:
/^(?:(http|https):\/\/[^\s"]*|\[MY_NAME\])+$/
I added parenthesis and an anti-selector ?: (This is assuming you want to match either string exactly)
Also I added a * to help your regex match correctly (as-well as a couple of other minor changes)
I'm trying to check for a url ending of either:
http://www.site.com/html/
OR
http://www.site.com/html/index.html
So far I have this (with numerous attempts of moving the $ and /'s) but can seem to get it to work.
window.location.pathname.match(/index.html/|/^$z/))
You could try this:
window.location.pathname.match(/\/$|index\.html/)
Will match the last / of the pathname, and also index.html
The first part of the regex "/$" escapes the forward slash, and the $ matches the last character of the string. So the way I read it is "The last character is a forwardslash"
The second part of the regex "index.html" matches index.html, but you have to escape the period because "." matches any character.
Heres a regular expression cheatsheet: http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/
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.