验证磁链的任何方法(JavaScript) [英] Any way to verifiy a magnet link (Javascript)
问题描述
可能是一个奇怪的问题,但我敢肯定有人曾经想到过这个问题:) 我想知道是否有可能使用JS将给定的字符串验证为理论上有效的磁铁链接.
Possibly an odd question, but I'm sure someone has thought of it before :) I'm wondering if it's at all possible to verify a given string as being a theoretically valid Magnet link, using JS.
不必特别担心打开链接等问题(在其他地方也可以做到),我在这里更关心清除断开/被截断的链接.
Not particularly bothered about opening the link etc. (That's done elsewhere), I'm more concerned here about weeding out broken/ truncated links.
我能想到的最好的方法是 magnet:?xt = urn:
The best I can come up with from the top of my head is a simple beginning of string match for the magnet:?xt=urn:
我想我可以用一个长度条件作为开头(似乎有20个以上的字符合理吗?),但是有人有更好的"解决方案吗?
I suppose I could preface this with a length condition (20+ characters seems reasonable?), but does anyone have a 'better' solution?
推荐答案
<!-- HTML -->
<div id="link">magnet:?xt=urn:3216546465987dfgs9798</div>
JavaScript:
The JavaScript:
var magnet_link = document.getElementById('link').innerHTML;
if (magnet_link.match(/magnet:\?xt=urn:[a-z0-9]{20,50}/i) != null) {
alert('Link is valid');
}
简短:
("magnet:?xt=urn:3216546465987dfgs9798".match(/magnet:\?xt=urn:[a-z0-9]{20,50}/i) != null)
这篇关于验证磁链的任何方法(JavaScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!