【问题标题】:RegEx for matching URLs with video IDs用于将 URL 与视频 ID 匹配的正则表达式
【发布时间】:2019-05-10 19:26:13
【问题描述】:

我正在尝试制作一个 iOS 快捷方式,它可以让我在新选项卡中一次打​​开所有网站视频链接,因此我需要一个正则表达式。

此网站上的视频链接如下所示:

https://m.worldstarhiphop.com/apple/video.php?v=wshhn5icmk9cKSyh9A17    
https://m.worldstarhiphop.com/apple/video.php?v=wshhc8Ew271C2BZE0l31

到目前为止我有这个:

^(?!image$).*(worldstarhiphop.com/apple/video)

因为我不希望快捷方式打开所有图片链接,而只打开视频链接。

【问题讨论】:

  • 到目前为止你有没有尝试过?我们可以查看您之前尝试的代码吗?
  • 我目前只有这个,因为我不想要打开图片链接的快捷方式,只想要视频链接:
  • 我无法粘贴我拥有的内容
  • 请将所有相关代码添加到问题正文中,而不是评论部分。
  • 是的,我已将其添加到帖子中

标签: ios regex regex-lookarounds regex-group regex-greedy


【解决方案1】:

我已在this expression 中添加了多个捕获组,以便于修改/更改和理解:

^((https?:\/\/.*)(worldstarhiphop.com)((\/apple\/video.php\?v=)|\/videos\/video.php\?v=)([A-Za-z0-9]{20}))

我没有使用$ 关闭右侧,如果您愿意,可以这样做。

您希望匹配的 URL 有两个移动版本和网络版本,我已经添加了各种协议,以防万一。如果没有必要,您可以将其删除。

正则表达式描述图

图表可视化了它的工作原理,您可能想在link 中测试其他表达式:

基本性能测试

此 JavaScript sn-p 返回运行时 100 万次 for 循环以提高性能。

const repeat = 1000000;
const start = Date.now();

for (var i = repeat; i >= 0; i--) {
	const string = 'https://m.worldstarhiphop.com/apple/video.php?v=wshhc8Ew271C2BZE0l31';
	const regex = /^((https?:\/\/.*)(worldstarhiphop.com)((\/apple\/video.php\?v=)|\/videos\/video.php\?v=)([A-Za-z0-9]{20}))/gm;
	var match = string.replace(regex, "\nGroup #1: $1\nGroup #2: $2 \nGroup #3: $3 \nGroup #4: $4\nGroup #6: $6 \n");
}

const end = Date.now() - start;
console.log("YAAAY! \"" + match + "\" is a match ??? ");
console.log(end / 1000 + " is the runtime of " + repeat + " times benchmark test. ? ");

【讨论】:

  • 再次感谢您的帮助
  • 它有点帮助,但是当我在网站上运行它时,没有任何链接出现......我觉得网站上有些东西阻止了正则表达式正常工作
  • 是的,看起来像它
猜你喜欢
  • 2022-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 2019-03-07
  • 2015-05-30
相关资源
最近更新 更多