簡體   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