Regex returns string undefined

前端 未结 4 1159
死守一世寂寞
死守一世寂寞 2021-01-29 13:23

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

var tesst = \"magnet:?xt=urn:btih:2B78EDFDDC87DC9605FB285997A80B787888C19         


        
4条回答
  •  臣服心动
    2021-01-29 14:08

    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) used for remove last '&', you can use any other method, like slice(0, 40), replace(/[^\w]/g, '') or any other.

提交回复
热议问题