用于匹配 YouTube 嵌入 ID 的正则表达式
RegEx for matching YouTube embed ID
我在非现代 JavaScript 并且我有一个字符串定义如下:
"//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0"
我只想拉出 DmYK479EpQc
但我不知道长度。我知道我想要 /
之后和 ?
之前的内容
是否有一些简单的 JavaScript 行可以解决这个问题?
一个选项使用正则表达式替换:
var url = "//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0";
var path = url.replace(/.*\/([^?]+).*/, "");
console.log(path);
上面的正则表达式模式表示:
.* match and consume everything up to and
/ including the last path separator
([^?]+) then match and capture any number of non ? characters
.* then consume the rest of the input
然后,我们只用第一个捕获组替换,它对应于最终路径分隔符之后的文本,但在查询字符串开始之前,URL应该有一个。
This expression 可能会帮助您这样做,而且可能会更快:
(d\/)([A-z0-9]+)(\?)
图表
此图显示了表达式的工作原理,您可以在此 link 中可视化其他表达式:
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const str = `//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0`;
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
性能测试
此 JavaScript 片段显示了使用简单的 100 万次 for
循环时该表达式的性能。
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const string = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
var match = string.replace(regex, "");
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");
我不打算给出一段代码,因为这是一个相对简单的算法,并且易于实现。
请注意这些链接的格式是这样的(如果我错了请纠正我):
https://
或 http://
www.youtube.com/
embed/
- 视频 ID(在本例中为
DmYK479EpQc
)
?parameters
(注意它们总是以字符 ?
开头)
您需要视频的 ID,因此您可以将字符串分成这些部分,如果您将这些部分存储在一个数组中,您可以确保 ID 位于第 3 个位置。
该数组的外观示例如下:
['https://', 'www.youtube.com', 'embed', 'DmYK479EpQc', '?vq=hd720&rel=0']
non-regex方式如何
console.log("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0".split('/').pop().split('?')[0]);
使用 URL
对象?
console.log(
(new URL("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0", location.href)).pathname
.split('/')
.pop());
为什么?因为我可能会编出一个 URL 来击败正则表达式(尽管对于 youtube 来说这不太可能)
您可以使用这个正则表达式
.*
匹配并消耗所有内容直到
[A-z0-9]+
然后匹配并捕获 A-z 之间的任意数字和字符
.*
然后消耗剩余的输入
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const position = '';
let result = ytUrl.replace(regex, position);
console.log('YouTube ID: ', result);
这个正则表达式只是将字符串分成不同的部分,YouTube ID 位于第 3 个位置。
另一个,解决方案是使用split
。此方法将字符串拆分为子字符串数组。
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
let result = ytUrl.split('/').pop().split('?').shift()
console.log('YouTube ID: ', result);
在此示例中,我们使用 /
作为分隔符拆分 URL。然后我们用 pop
方法取出数组的最后一个元素。最后我们使用 ?
作为分隔符再次拆分,我们使用 shift
方法获取数组的第一个元素。
我在非现代 JavaScript 并且我有一个字符串定义如下:
"//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0"
我只想拉出 DmYK479EpQc
但我不知道长度。我知道我想要 /
之后和 ?
是否有一些简单的 JavaScript 行可以解决这个问题?
一个选项使用正则表达式替换:
var url = "//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0";
var path = url.replace(/.*\/([^?]+).*/, "");
console.log(path);
上面的正则表达式模式表示:
.* match and consume everything up to and
/ including the last path separator
([^?]+) then match and capture any number of non ? characters
.* then consume the rest of the input
然后,我们只用第一个捕获组替换,它对应于最终路径分隔符之后的文本,但在查询字符串开始之前,URL应该有一个。
This expression 可能会帮助您这样做,而且可能会更快:
(d\/)([A-z0-9]+)(\?)
图表
此图显示了表达式的工作原理,您可以在此 link 中可视化其他表达式:
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const str = `//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0`;
const subst = ``;
// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);
console.log('Substitution result: ', result);
性能测试
此 JavaScript 片段显示了使用简单的 100 万次 for
循环时该表达式的性能。
const repeat = 1000000;
const start = Date.now();
for (var i = repeat; i >= 0; i--) {
const string = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
var match = string.replace(regex, "");
}
const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ");
我不打算给出一段代码,因为这是一个相对简单的算法,并且易于实现。
请注意这些链接的格式是这样的(如果我错了请纠正我):
https://
或http://
www.youtube.com/
embed/
- 视频 ID(在本例中为
DmYK479EpQc
) ?parameters
(注意它们总是以字符?
开头)
您需要视频的 ID,因此您可以将字符串分成这些部分,如果您将这些部分存储在一个数组中,您可以确保 ID 位于第 3 个位置。
该数组的外观示例如下:
['https://', 'www.youtube.com', 'embed', 'DmYK479EpQc', '?vq=hd720&rel=0']
non-regex方式如何
console.log("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0".split('/').pop().split('?')[0]);
使用 URL
对象?
console.log(
(new URL("//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0", location.href)).pathname
.split('/')
.pop());
为什么?因为我可能会编出一个 URL 来击败正则表达式(尽管对于 youtube 来说这不太可能)
您可以使用这个正则表达式
.*
匹配并消耗所有内容直到
[A-z0-9]+
然后匹配并捕获 A-z 之间的任意数字和字符
.*
然后消耗剩余的输入
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
const regex = /(.*)(d\/)([A-z0-9]+)(\?)(.*)/gm;
const position = '';
let result = ytUrl.replace(regex, position);
console.log('YouTube ID: ', result);
这个正则表达式只是将字符串分成不同的部分,YouTube ID 位于第 3 个位置。
另一个,解决方案是使用split
。此方法将字符串拆分为子字符串数组。
const ytUrl = '//www.youtube.com/embed/DmYK479EpQc?vq=hd720&rel=0';
let result = ytUrl.split('/').pop().split('?').shift()
console.log('YouTube ID: ', result);
在此示例中,我们使用 /
作为分隔符拆分 URL。然后我们用 pop
方法取出数组的最后一个元素。最后我们使用 ?
作为分隔符再次拆分,我们使用 shift
方法获取数组的第一个元素。