comparison src/test/tiny.html @ 30:8ff35379cc89

regex cleanup
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 25 Jul 2022 22:10:28 -0600
parents d9d7aa2a79db
children 6d265f5e18e2
comparison
equal deleted inserted replaced
29:a1db5223ced1 30:8ff35379cc89
8 <script> 8 <script>
9 function videoIframe(url) { 9 function videoIframe(url) {
10 return '<iframe data-video="'+url+'" width="560" height="315" frameborder="0" allowfullscreen src="'+url+'"></iframe>'; 10 return '<iframe data-video="'+url+'" width="560" height="315" frameborder="0" allowfullscreen src="'+url+'"></iframe>';
11 } 11 }
12 12
13 // fucking moronic javascript doesn't have \Q \E in regex
13 var videoHandlers = {}; 14 var videoHandlers = {};
14 { 15 {
15 let ptn1 = new RegExp('https://youtu.be/([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+))?'); 16 let ptn1 = /^https:\/\/youtu\.be\/([a-zA-Z0-9_-]+)(?:\?t=([0-9]+))?/;
16 let ptn2 = new RegExp('https://www.youtube.com/watch\\?v=([a-zA-Z0-9_-]+)(?:\\?t=([0-9]+)s)?'); 17 let ptn2 = /^https:\/\/www\.youtube\.com\/watch\?v=([a-zA-Z0-9_-]+)(?:&t=([0-9]+)s)?/;
17 videoHandlers.youtube = function(url) { 18 videoHandlers.youtube = function(url) {
18 let result = url.match(ptn1) || url.match(ptn2); 19 let result = url.match(ptn1) || url.match(ptn2);
19 if( result ) { 20 if( result ) {
20 url = 'https://www.youtube.com/embed/' + result[1]; 21 url = 'https://www.youtube.com/embed/' + result[1];
21 if( result[2] ) 22 if( result[2] )
23 return videoIframe(url); 24 return videoIframe(url);
24 } 25 }
25 } 26 }
26 } 27 }
27 { 28 {
28 let ptn = new RegExp('https://rumble.com/embed/[a-z0-9]+/\\?pub=[a-z0-9]+'); 29 let ptn = /^https:\/\/rumble\.com\/embed\/[a-z0-9]+\/\?pub=[a-z0-9]+/;
29 videoHandlers.rumble = function(url) { 30 videoHandlers.rumble = function(url) {
30 if( url.match(ptn) ) { 31 if( url.match(ptn) ) {
31 return videoIframe(url); 32 return videoIframe(url);
32 } 33 }
33 } 34 }
34 } 35 }
35 { 36 {
36 let ptn = new RegExp('https://www.bitchute.com/video/([a-zA-Z0-9]+)/'); 37 let ptn = /^https:\/\/www\.bitchute\.com\/video\/([a-zA-Z0-9]+)\//;
37 videoHandlers.bitchute = function(url) { 38 videoHandlers.bitchute = function(url) {
38 let result = url.match(ptn); 39 let result = url.match(ptn);
39 if( result ) { 40 if( result ) {
40 url = 'https://www.bitchute.com/embed/' + result[1]; 41 url = 'https://www.bitchute.com/embed/' + result[1];
41 return videoIframe(url); 42 return videoIframe(url);
42 } 43 }
43 } 44 }
44 } 45 }
45 { 46 {
46 let ptn = new RegExp('https://vimeo.com/([0-9]+)'); 47 let ptn = /^https:\/\/vimeo\.com\/([0-9]+)/;
47 videoHandlers.vimeo = function(url) { 48 videoHandlers.vimeo = function(url) {
48 let result = url.match(ptn); 49 let result = url.match(ptn);
49 if( result ) { 50 if( result ) {
50 url = 'https://player.vimeo.com/video/' + result[1]; 51 url = 'https://player.vimeo.com/video/' + result[1];
51 return videoIframe(url); 52 return videoIframe(url);
52 } 53 }
53 } 54 }
54 } 55 }
55 { 56 {
56 let ptn = new RegExp('https://dai.ly/([a-z0-9]+)'); 57 let ptn = /^https:\/\/dai\.ly\/([a-z0-9]+)/;
57 videoHandlers.dailymotion = function(url) { 58 videoHandlers.dailymotion = function(url) {
58 let result = url.match(ptn); 59 let result = url.match(ptn);
59 if( result ) { 60 if( result ) {
60 url = 'https://www.dailymotion.com/embed/video/' + result[1]; 61 url = 'https://www.dailymotion.com/embed/video/' + result[1];
61 return videoIframe(url); 62 return videoIframe(url);
62 } 63 }
63 } 64 }
64 } 65 }
65 { 66 {
66 let ptn = new RegExp('https://www.tiktok.com/[^/]+/video/([0-9]+)'); 67 let ptn = /^https:\/\/www\.tiktok\.com\/[^/]+\/video\/([0-9]+)/;
67 videoHandlers.tiktok = function(url) { 68 videoHandlers.tiktok = function(url) {
68 let result = url.match(ptn); 69 let result = url.match(ptn);
69 if( result ) { 70 if( result ) {
70 let html = '<blockquote class="tiktok-embed" data-video-id="'+result[1]+'" style="max-width: 560px; margin-left: 0;"><section></section></blockquote>'; 71 let html = '<blockquote class="tiktok-embed" data-video-id="'+result[1]+'" style="max-width: 560px; margin-left: 0;"><section></section></blockquote>';
71 //html += '<script async src="https://www.tiktok.com/embed.js"></'+'script>'; 72 //html += '<script async src="https://www.tiktok.com/embed.js"></'+'script>';
72 return html; 73 return html;
73 } 74 }
74 } 75 }
75 } 76 }
76 { 77 {
77 let ptn = new RegExp('\\.[a-zA-Z0-9]+$'); 78 let ptn = /\.[a-zA-Z0-9]+$/;
78 videoHandlers.file = function(url) { 79 videoHandlers.file = function(url) {
79 if( url.match(ptn) ) { 80 if( url.match(ptn) ) {
80 return '<video controls width="560" height><source src="'+url+'"></video>'; 81 return '<video controls width="560" height><source src="'+url+'"></video>';
81 } 82 }
82 } 83 }