Extracting parts of the string between two special character - Javascript

I have a complex string which i get as a part of my response. but i need to extract parts of strings between special characters i.e starting special character -> and ending special character <-. Anything between these tags needs to be displayed rest should be ignored

String to be changed

"validation error [claimDto:Reporter.HomeNumber->Value entered must be a valid phone number<-, claimDto:Lobs.PostalCode->Please enter a valid ZIP code.<-, claimDto:Lobs.HomeNumber->Value entered must be a valid phone number]"

Expected Value:

Value entered must be a valid phone number
Please enter a valid ZIP code.

2 answers

  • answered 2018-11-08 06:12 CertainPerformance

    A simple match would do it, lookbehind for ->, lookahead for <-, and use a global flag:

    const input = "validation error [claimDto:Reporter.HomeNumber->Value entered must be a valid phone number<-, claimDto:Lobs.PostalCode->Please enter a valid ZIP code.<-, claimDto:Lobs.HomeNumber->Value entered must be a valid phone number]";
    console.log(input.match(/(?<=->).*?(?=<-)/g));

    Some browsers don't support lookbehind yet - without using lookbehind, match the opening arrow instead of lookbehind, iterate over every match and extract the group:

    const input = "validation error [claimDto:Reporter.HomeNumber->Value entered must be a valid phone number<-, claimDto:Lobs.PostalCode->Please enter a valid ZIP code.<-, claimDto:Lobs.HomeNumber->Value entered must be a valid phone number]";
    let match;
    const re = /->(.*?)(?=<-)/g;
    const output = [];
    while (match = re.exec(input)) {
      output.push(match[1]);
    }
    console.log(output);

  • answered 2018-11-08 06:41 Sly_cardinal

    You can get close to @CertainPerformance's answer without needing lookbehind support:

    const input = "validation error [claimDto:Reporter.HomeNumber->Value entered must be a valid phone number<-, claimDto:Lobs.PostalCode->Please enter a valid ZIP code.<-, claimDto:Lobs.HomeNumber->Value entered must be a valid phone number]";
    
    const matchText = /->(.+)/;
    input.split('<-')
        .map(s => matchText.exec(s))
        .filter(x => x)
        .map(x => x[1]);
    

    Result:

    [
        "Value entered must be a valid phone number",
        "Please enter a valid ZIP code.",
        "Value entered must be a valid phone number]"
    ]
    

    Is your input string missing a final <-? That's what's causing the trailing ] character ;)

    Explanation:

    input.split('<-')   // split the string into segments that end with '<-' (removes the '<-' characters too)
    
    .map(s => /->(.+)/.exec(s))    // capture any text after '->' to the end of the string. Any strings without '->' in them will not match and will return null
    
    .filter(x => x) // filter out any null regexp results    
    
    .map(x => x[1]); // put out the captured text from each regexp result (x[0] is the entire match, x[1] is just the captured text)