2

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.

Baku Bakar
  • 442
  • 2
  • 8
  • 20
  • 1
    _" 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. – Andreas Dec 15 '18 at 13:17
  • Good point, but I have to. Reason: I have to display it, even I am working only with one element. – Baku Bakar Dec 15 '18 at 13:40
  • https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – epascarello Dec 15 '18 at 13:59

3 Answers3

1

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);
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 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? – Baku Bakar Dec 15 '18 at 13:50
  • @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. – T.J. Crowder Dec 15 '18 at 13:52
1

but the second task I didn't.

You can use filter() and startsWith()

splittedResult = splittedResult.filter(i => i.startsWith('path_of_code='));
Giulio Bambini
  • 4,695
  • 4
  • 21
  • 36
1

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)
Mohammad
  • 21,175
  • 15
  • 55
  • 84