0

I have a node script that reads another js file, and I want to get the value of a specific variable in that js file. As I can imagine, there are way better solutions to solve exactly my problem, but I want a function, that I can use again later on, in for example web-apps.

I need a function, that returns the string between a X string and a Y string. For example:

const string2searchin = "<random text with unknown length> /start/ i want to be selected /end/ <random text with unknown length>"

How can I get the string that is between "start" and "end"? The length of the random text it is between is not known.

I've tried this:

var getText = (string, start, end) => {
    let arr = string.split("");
    let listen = false;
    let output = [];

    for(let el of arr){
        if(el == end && listen) return output.toString();
        if(listen) output.push(el);
        if(el == start && !listen) listen = true;
    }
} 

It works just fine, but only with single characters. I want to have string with a length greater than 1 as a start and as an end.

by James
  • 89
  • 10
  • 4
    Please post what you've attempted and the errors you've encountered. – John May 07 '19 at 18:06
  • 1
    Can you show what code have you tried, please? – Jorge Fuentes González May 07 '19 at 18:06
  • 1
    .You can provide `slice()` the sum of indexes of both `x` and `y` and their length. `str.slice(str.indexOf(x) + str.length, str.indexOf(y) + y.length + 1)` – Maheer Ali May 07 '19 at 18:07
  • 1
    Well, it wouldn't hurt explaining your problem (or rather your expected behaviour) a bit better, because the question is actually not phrased very clear. Do you want to get the , or the text in between? Because how is the code supposed to know ehere the random text starts and ends, unless you define what the text in between looks like (which doesn't make sense, cause then you'd already know what text you're looking for and wouldn't need to extract it...) – Constantin Groß May 07 '19 at 18:08
  • https://stackoverflow.com/questions/14867835/get-substring-between-two-characters-using-javascript – Peter Szalay May 07 '19 at 18:09
  • var matches = string2searchin.match(/\/start\/(.*)\/end\//); – jgroenen May 07 '19 at 18:15
  • JohnD I updated the question, I didn't write it like this in the first place because I know there will be answers for my specific js-file problem, but not my real question. – by James May 07 '19 at 18:15

5 Answers5

0

Sounds like you might be looking for .substring() ? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring

you could write your own function that takes in 2 number, start and stop point for the substring function.

Arron McCrory
  • 690
  • 7
  • 15
  • I know .substring(), but it's not what I looked for. I want to get the text using strings, not indexes(numbers). I updated the question for a better understanding. – by James May 07 '19 at 18:09
  • the 'random text' you mention, is it a particular word, or is it a bunch of random words? I have a feeling the first thing you are going to want to do is string.split(' ') to create an array of every word in your string. then you look for the particular start and end words in that array, then just return everything between those two points. – Arron McCrory May 07 '19 at 18:20
  • the random text is a bunch of random words with a unknown length. Good Idea with string.split(" "), but what if I have for example *start, it would detect it as "start". – by James May 07 '19 at 18:43
0

var x = "start";
var y = "end";
var str = "<random text> start i want to be selected end <random text>";
console.log(str.substring(str.indexOf(x) + x.length, str.lastIndexOf(y)));
Alejandro Camba
  • 978
  • 10
  • 25
0

you can use regex as well.Try this

const regex = /start(.*)end/gm;
const str = `<random text> start i want to be selected end <random text>`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }

    // This result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}
Ashok
  • 743
  • 4
  • 13
0

Here is a solution that suppose a <random text> doesn't contain start and end.

const string2searchin = "<random text> start i want to be selected end <random text>";
s = string2searchin.split('end')[0].split('start')[1].trim();
Banzay
  • 9,310
  • 2
  • 27
  • 46
0

Sorry, this is a duplicate of Get Substring between two characters using javascript I modified the answer from there a little bit to fit my purpose:

var findText = (string, x, y) => string.substring(string.lastIndexOf(x) + x.length, string.lastIndexOf(y));
by James
  • 89
  • 10