简体   繁体   中英

Validate url with extensios at the end using regular expression

How to validate the below url's which is ending with extensions like .mp4 or .wmv

And also starting with http or https or www

Example:

http://media.ch9.ms/ch9/360b/74fd8811-951f-40aa-bc24-91d51b82360b/Search.mp4
https://media.ch9.ms/ch9/360b/74fd8811-951f-40aa-bc24-91d51b82360b/Search.mp4
www.media.ch9.ms/ch9/360b/74fd8811-951f-40aa-bc24-91d51b82360b/Search.mp4

More readable:

^(http:\/\/|https:\/\/|www\.).*(\.mp4|\.mkv)$

More concise:

^(http(s)?:\/\/|www\.).*(\.mp4|\.mkv)$

Demo:

https://regex101.com/r/wK2rV0/1

Explanation:

  • First group verifies that the line starts "^" matching "http://"(with optional: https) or www.
  • in the middle, ".*", any given character different to newline
  • the last group, it must match or mp4 or mkv at the end of the line.
/^(http[s]?:\/\/)?([^:\/\s]+)(:([^\/]*))?(\/\w+\.)*([^#?\s]+)(\?([^#]*))?(\.mp4|\.mkv)$/gm

在此处查看演示https://regex101.com/r/vL1gZ5/2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM