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

what is in javascript
in javascript
... in javascript es6
in javascript
question mark in javascript
javascript syntax
javascript modulus operator
javascript

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);
}

What is JavaScript Used For?, gives web pages interactive elements that engage a user. JavaScript is a scripting or programming language that allows you to implement complex features on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved.

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)

What Is JavaScript Used For: Find Out What Can You Do With , is used to create responsive, interactive elements for web pages, enhancing the user experience. JavaScript (/ ˈ dʒ ɑː v ə ˌ s k r ɪ p t /), often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm.

but the second task I didn't.

You can use filter() and startsWith()

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

JavaScript Increment ++ and Decrement --, will add one (+1) or subtract one (-1), respectively, to their operand, and then return a value. For example in the math equation 1 + 2, both 1 and 2 are operands, while + is the operator. The JavaScript this keyword refers to the object it belongs to. It has different values depending on where it is used: In a method, this refers to the owner object. Alone, this refers to the global object. In a function, this refers to the global object. In a function, in strict mode, this is undefined. In an event, this refers to the element that received the event.

JavaScript Operators Reference, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  js, or "j.s." is short for "just saying." It's a shortening of the word for texting but is also used in everyday language, just as "omg" has become. It is usually used at the end of a sentence, and commonly in a sentence where you are making a rude or mean comment.

Expressions and operators, Your snippet of code looks like it's referencing methods from one of the popular JavaScript libraries (jQuery, ProtoType, mooTools, and so on). A JS file is a text file containing JavaScript code that is used to execute JavaScript instructions in web pages. It may include functions that open and close windows, validate form fields, enable rollover images, or create drop-down menus. More Information. JS files are referenced by web pages that incorporate JavaScript functions.

What is JavaScript?, js is a JavaScript runtime environment. JavaScript now has the capability to do things that other scripting languages like Python can do. Both your browser JavaScript and Node. js run on the V8 JavaScript runtime engine. This engine takes your JavaScript code and converts it into a faster machine code. The dollar sign ($) and the underscore (_) characters are JavaScriptidentifiers, which just means that they identify an object in the same way a name would.  The objects they identify include things such as variables, functions, properties, events, and objects.

Comments
  • " replace all & with an break (\n) first" - Why? This operation on its own doesn't make much sense or would in any way make the task even easier.
  • Good point, but I have to. Reason: I have to display it, even I am working only with one element.
  • stackoverflow.com/questions/901115/…
  • Thank you very much! It works, but how can I handle, if there are two "path_of_code" ? If there are two of them, I have to work with both of them. but sparatly. I think the best way would be, write the result into an array, what do you think?
  • @EmbaBakar - If you add a g flag to the regex, you can call exec again on the same string to get the next occurrence. So you could loop until exec returns null to get all the occurrences. What you do with each occurrence is up to you: Process it as you go through the string, or store them in an array and process them afterward, or... It's up to you.