Mercurial Hosting > freedit
changeset 30:8ff35379cc89
regex cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 25 Jul 2022 22:10:28 -0600 |
parents | a1db5223ced1 |
children | 6d265f5e18e2 |
files | src/lib/Bbcode.luan src/test/tiny.html |
diffstat | 2 files changed, 16 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
diff -r a1db5223ced1 -r 8ff35379cc89 src/lib/Bbcode.luan --- a/src/lib/Bbcode.luan Mon Jul 25 21:28:10 2022 -0600 +++ b/src/lib/Bbcode.luan Mon Jul 25 22:10:28 2022 -0600 @@ -109,8 +109,8 @@ local video_handlers = {} do - local ptn1 = regex[[https://youtu.be/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]] - local ptn2 = regex[[https://www.youtube.com/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]] + local ptn1 = regex[[^\Qhttps://youtu.be/\E([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?]] + local ptn2 = regex[[^\Qhttps://www.youtube.com/watch?v=\E([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?]] function video_handlers.youtube(url) local id, start = ptn1.match(url) if id == nil then @@ -128,7 +128,7 @@ end end do - local ptn = regex[[https://rumble.com/embed/[a-z0-9]+/\?pub=[a-z0-9]+]] + local ptn = regex[[^\Qhttps://rumble.com/embed/\E[a-z0-9]+/\?pub=[a-z0-9]+]] function video_handlers.rumble(url) if not ptn.matches(url) then return false @@ -138,7 +138,7 @@ end end do - local ptn = regex[[https://www.bitchute.com/video/([a-zA-Z0-9]+)/]] + local ptn = regex[[^\Qhttps://www.bitchute.com/video/\E([a-zA-Z0-9]+)/]] function video_handlers.bitchute(url) local id = ptn.match(url) if id == nil then @@ -150,7 +150,7 @@ end end do - local ptn = regex[[https://vimeo.com/([0-9]+)]] + local ptn = regex[[^\Qhttps://vimeo.com/\E([0-9]+)]] function video_handlers.vimeo(url) local id = ptn.match(url) if id == nil then @@ -162,7 +162,7 @@ end end do - local ptn = regex[[https://dai.ly/([a-z0-9]+)]] + local ptn = regex[[^\Qhttps://dai.ly/\E([a-z0-9]+)]] function video_handlers.dailymotion(url) local id = ptn.match(url) if id == nil then @@ -174,7 +174,7 @@ end end do - local ptn = regex[[https://www.tiktok.com/[^/]+/video/([0-9]+)]] + local ptn = regex[[^\Qhttps://www.tiktok.com/\E[^/]+/video/([0-9]+)]] function video_handlers.tiktok(url) local id = ptn.match(url) if id == nil then
diff -r a1db5223ced1 -r 8ff35379cc89 src/test/tiny.html --- a/src/test/tiny.html Mon Jul 25 21:28:10 2022 -0600 +++ b/src/test/tiny.html Mon Jul 25 22:10:28 2022 -0600 @@ -10,10 +10,11 @@ return '<iframe data-video="'+url+'" width="560" height="315" frameborder="0" allowfullscreen src="'+url+'"></iframe>'; } + // fucking moronic javascript doesn't have \Q \E in regex var videoHandlers = {}; { - let ptn1 = new RegExp('https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?'); - let ptn2 = new RegExp('https://www.youtube.com/watch\\?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?'); + let ptn1 = /^https:\/\/youtu\.be\/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?/; + let ptn2 = /^https:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?/; videoHandlers.youtube = function(url) { let result = url.match(ptn1) || url.match(ptn2); if( result ) { @@ -25,7 +26,7 @@ } } { - let ptn = new RegExp('https://rumble.com/embed/[a-z0-9]+/\\?pub=[a-z0-9]+'); + let ptn = /^https:\/\/rumble\.com\/embed\/[a-z0-9]+\/\?pub=[a-z0-9]+/; videoHandlers.rumble = function(url) { if( url.match(ptn) ) { return videoIframe(url); @@ -33,7 +34,7 @@ } } { - let ptn = new RegExp('https://www.bitchute.com/video/([a-zA-Z0-9]+)/'); + let ptn = /^https:\/\/www\.bitchute\.com\/video\/([a-zA-Z0-9]+)\//; videoHandlers.bitchute = function(url) { let result = url.match(ptn); if( result ) { @@ -43,7 +44,7 @@ } } { - let ptn = new RegExp('https://vimeo.com/([0-9]+)'); + let ptn = /^https:\/\/vimeo\.com\/([0-9]+)/; videoHandlers.vimeo = function(url) { let result = url.match(ptn); if( result ) { @@ -53,7 +54,7 @@ } } { - let ptn = new RegExp('https://dai.ly/([a-z0-9]+)'); + let ptn = /^https:\/\/dai\.ly\/([a-z0-9]+)/; videoHandlers.dailymotion = function(url) { let result = url.match(ptn); if( result ) { @@ -63,7 +64,7 @@ } } { - let ptn = new RegExp('https://www.tiktok.com/[^/]+/video/([0-9]+)'); + let ptn = /^https:\/\/www\.tiktok\.com\/[^/]+\/video\/([0-9]+)/; videoHandlers.tiktok = function(url) { let result = url.match(ptn); if( result ) { @@ -74,7 +75,7 @@ } } { - let ptn = new RegExp('\\.[a-zA-Z0-9]+$'); + let ptn = /\.[a-zA-Z0-9]+$/; videoHandlers.file = function(url) { if( url.match(ptn) ) { return '<video controls width="560" height><source src="'+url+'"></video>';