繁体   English   中英

JS显示列表的特定行的最快方法是什么?

[英]JS What's the fastest way to display one specific line of a list?

在我的Javascript代码中,我得到了很长的一行作为字符串。 这一行只有大约65'000个字母。 例:

config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...

我要做的是先将所有&替换为一个换行符(\\ n) ,然后仅选择以“ path_of_code =”开头的行 这行我必须写一个变量。

带有替换&的部分(\\ n)我已经知道了,但是第二项任务没有。

    var obj = document.getElementById('div_content');
    var contentJS= obj.value;
    var splittedResult;
    splittedResult = contentJS.replace(/&/g, '\n');

最快的方法是什么? 请注意,列表通常很长。

听起来您想提取&path_of_code=之后的文本,直到字符串的末尾或下一个&为止。 通过使用捕获组的正则表达式,然后使用该捕获组的值,可以轻松完成此操作:

var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
    var text = match[1];
}

现场示例:

 var theString = "config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs..."; var rex = /&path_of_code=([^&]+)/; var match = rex.exec(theString); if (match) { var text = match[1]; console.log(text); } 

但是第二个任务我没有。

您可以使用filter()startsWith()

splittedResult = splittedResult.filter(i => i.startsWith('path_of_code='));

结合使用String.indexOf()String.substr()

 var contentJS= "123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs..."; var index = contentJS.indexOf("&path_of_code"), substr = contentJS.substr(index+1), res = substr.substr(0, substr.indexOf("&")); console.log(res) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM