Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 匹配字符串并获得匹配后的内容_Javascript_Jquery_Regex - Fatal编程技术网

Javascript 匹配字符串并获得匹配后的内容

Javascript 匹配字符串并获得匹配后的内容,javascript,jquery,regex,Javascript,Jquery,Regex,我有一个torrent文件中的内容 d8:announce39:http://torrent.ubuntu.com:6969/announce13:announce-listll39:http://torrent.ubuntu.com:6969/announceel44:http://ipv6.torrent.ubuntu.com:6969/announceee7:comment29:Ubuntu CD releases.ubuntu.com13:creation datei1539860537

我有一个torrent文件中的内容

d8:announce39:http://torrent.ubuntu.com:6969/announce13:announce-listll39:http://torrent.ubuntu.com:6969/announceel44:http://ipv6.torrent.ubuntu.com:6969/announceee7:comment29:Ubuntu CD releases.ubuntu.com13:creation datei1539860537e4:infod6:lengthi1999503360e4:name30:ubuntu-18.10-desktop-amd64.iso12:piece lengthi524288e6:pieces76280
或者我也可以

d8:announce39:http://torrent.centos.org:6969/announce13:announce-listll39:http://torrent.centos.org:6969/announceel44:http://ipv6.torrent.centos.org:6969/announceee7:comment27:CentOS x86_64 LiveGNOME ISO10:created by13:mktorrent 1.013:creation datei1526053398e4:infod5:filesld6:lengthi1388314624e4:pathl34:CentOS-7-x86_64-LiveGNOME-1804.isoeed6:lengthi454e4:pathl11:sha1sum.txteed6:lengthi1314e4:pathl15:sha1sum.txt.asceed6:lengthi598e4:pathl13:sha256sum.txteed6:lengthi1458e4:pathl17:sha256sum.txt.asceee4:name30:CentOS-7-x86_64-LiveGNOME-180412:piece lengthi524288e6:pieces52980:
我想检索名称部分,如
ubuntu-18.10-desktop-amd64.iso
以下是我尝试的内容,但它检查了全部内容:

$.get('search', function(data) {
    var lines = data.split("\n");
    var $result = false
    var url_check 
    var search= [];
    $.each(lines, function(n, data) {
        search.push(data)
    })
}).done(function(search){
    search= search.split("\n");
    var $result = [];
    $.each(search, function(n, search) {
        var regex = new RegExp('^(?=.*' + search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').split(/\\?[\s,_.:*-]+/).join(')(?=.*') + ')', 'gi');
        if(regex.test(url_check) === true){
            $result.push('true');
        }else{
            $result.push('false');
        }
    })
    console.log($result)
    if($result.indexOf("true") !== -1){
        alert('Found !') 
    }else {
        alert('Not found !');
    }
})
文件内容搜索

*Ubuntu.18*
*centos 7*
根据,这些尾随数字与以下字符串的长度匹配。比如说

ubuntu-18.10-desktop-amd64.iso
长度为30个字符,因此前面的数字为30:

30:ubuntu-18.10-desktop-amd64.iso
因此,一个纯粹的正则表达式解决方案不会真正起作用,至少不会以任何简洁的方式起作用。您可以提取
名称:
及其后面的字符串的其余部分,然后在Javascript中使用
切片
提取冒号后面的
字符数:

const输入=[
'd8:39:http://torrent.ubuntu.com:6969/announce13:announce-清单39:http://torrent.ubuntu.com:6969/announceel44:http://ipv6.torrent.ubuntu.com:6969/announceee7:comment29:Ubuntu CD releases.ubuntu.com13:creation datei1539860537e4:infod6:lengthi1999503360e4:name30:ubuntu-18.10-desktop-amd64.iso12:piece lengthi524288e6:pieces76280',
'd8:39:http://torrent.centos.org:6969/announce13:announce-清单39:http://torrent.centos.org:6969/announceel44:http://ipv6.torrent.centos.org:6969/announceee7:comment27:CentOS x86_64 LiveGNOME ISO10:创建人13:mktorrent 1.013:创建日期1526053398e4:infod5:filesld6:lengthi1388314624e4:pathl34:CentOS-7-x86_64-LiveGNOME-1804.等电点6:lengthi454e4:pathl11:sha1sum.txted6:lengthi1314e4:pathl15:sha1sum.txt.asceed6:lengthi598e4:pathl13:sha256sum.txted6:lengthi1458e4:pathl17:sha256sum.txt.ascee4:name30:CentOS-7-x86_64-LiveGNOME-180412:pieclengthi524288e6:pieces52980:'
];
函数getName(str){
const match=str.match(/:name(\d+):(.+)$/);
如果(!匹配){
返回console.log(“找不到名称”);
}
常数[,长度,剩余]=匹配;
console.log(
rest.slice(0,长度)
);
}

input.forEach(getName)
:piece
之前是否总是正好有两位数字,比如
12:piece
?(对于第二个示例文本,我假设您想要
CentOS-7-x86_64-LiveGNOME-1804
,对吗?)这个
ubuntu-18.10-desktop-amd64.iso
是否总是跟在
piece
后面?不,那么您如何区分名称的部分(例如
1804
)和后面的数字(即
12
)?如果没有某种模式,就无法匹配那里的名称和其他任何内容it@executable正如我所说,考虑到当前的信息,这个问题并不是完全可以解决的——如果不知道从名称内容中分离不需要的尾随数字的逻辑,就无法编写在一般情况下有效的正则表达式。请检查使用我得到的函数的尾随数字的数量是否有任何逻辑。match不是函数或其返回值不可编辑听起来好像输入字符串与
:name\d+:
模式不匹配,你可以在解构之前添加一个匹配对象真实性测试,如果我在函数中像
console.log('ok'+str)
那样记录,我会得到这个结果。我不明白有什么不对,是把绳子收回来了。代替
常量输入
可以演示如何在简单字符串上使用它吗?听起来字符串的其余部分包含
\r
\n
s,而
不匹配-请参见编辑,使用
[\s\s]
而不是
要完全匹配任何字符,如果您想让过时的浏览器理解您的代码,可以使用Babel将ES6+语法自动传输到ES5。该行的等效值为
var length=match[1];var rest=匹配[2]