In my Javascript code, I get one very long line as a string. This one line only has around 65'000 letters. Example:
config=123&url=http://localhost/example&path_of_code=blablaba&link=kjslfdjs...
What I have to do is replace all & with an break (\\n) first and then pick only the line which starts with "path_of_code=" . This line I have to write in a variable.
The part with replace & with an break (\\n) I already get it, but the second task I didn't.
var obj = document.getElementById('div_content');
var contentJS= obj.value;
var splittedResult;
splittedResult = contentJS.replace(/&/g, '\n');
What is the fastest way to do it? Please note, the list is usually very long.
It sounds like you want to extract the text after &path_of_code=
up until either the end of the string or the next &
. That's easily done with a regular expression using a capture group, then using the value of that capture group:
var rex = /&path_of_code=([^&]+)/;
var match = rex.exec(theString);
if (match) {
var text = match[1];
}
Live Example:
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); }
but the second task I didn't.
You can use filter()
and startsWith()
splittedResult = splittedResult.filter(i => i.startsWith('path_of_code='));
Use combination of String.indexOf()
and 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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.