正则表达式返回未定义的字符串 [英] Regex returns string undefined

查看:23
本文介绍了正则表达式返回未定义的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从磁力链接中提取哈希值,但它返回 undefined

I am trying to extract hash value from an magnet link but it returns undefined

var tesst = "magnet:?xt=urn:btih:2B78EDFDDC87DC9605FB285997A80B787888C194&"
var test = tesst.match(/magnet:\?xt=urn:btih:[a-z\d]{40}\&/im);
alert (test[1]);

我不明白我做错了什么.

I cant understand what I am doing wrong.

推荐答案

用捕获组标记你想要的:

just mark what you want with capturing group:

/^magnet:\?xt=urn:btih:([a-z\d]{40})\&$/im

另外我建议不要在这里使用正则表达式.
尝试遵循:

Also I recomend to not use regexp here.
Try followed:

tesst.split(':')[3].slice(0, -1);

slice(0, -1) 用于删除最后一个 '&',您可以使用任何其他方法,例如 slice(0, 40), replace(/[^\w]/g, '') 或任何其他.

slice(0, -1) used for remove last '&', you can use any other method, like slice(0, 40), replace(/[^\w]/g, '') or any other.

这篇关于正则表达式返回未定义的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆