Match index.html or empty url end with regex - javascript

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/

Related

Regex for matching BBCode Images

I need a regex to verify if the textarea has one of the following matches:
[img]https://example.com/image.jpg[/img]
[img=https://example.com/image.jpg]
This is what I've been trying so far, but it doesn't work, sadly...
/\[img(?=|\])(https?:\/\/(www\.)?[-a-zA-Z0-9#:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()#:%_\+.~#?&//=]*))(?\])(?\[\/img\])/gi
Thank you.
You can use this- ^\[img(?:\].+\[\/img\]|=.+\])$
Note: If you want to verify the link string is a valid URL, replace both .+ with a URL regex matcher, you may find one here
Explanation
^\[img - This part is common both strings, this will match the [img at the start of line
(?:\].+\[\/img\]|=.+\])$ - This will match 2 alternatives, depending on the very first character
First alternative (first character is ]) - In this case \].+\[\/img\]| will be matched. This will match everything (.+) in between the opening and closing [img] tags before finally matching the closing tag itself.
Second alternative (first character is =) - In this case =.+\] will be matched. This grabs everything after img= and stops when ] is reached.
finally the regex matches the end of line.
Check out the demo

Regex finding file names

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/

Regex Positive lookahead get first occurrence

I Have String like this, and I want capture characters between .html and the first slash
http://example.org/some-path/some-title-in-1978.html
This part some-title-in-1978, for that I came up with this regex:
/\/.+?(?=\.html)/ and result are not what i want, it's like this:
//domain.org/some-path/some-title-in-1978
Use the following regex pattern:
[^/]+(?=\.html)
https://regex101.com/r/wep2Im/1
[^/]+ - matches all characters that are followed by .html except forward slash

regex - how to select all double slashes except followed by colon

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,}/

Regex for fragment url, but not whole

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}$

Categories