0

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.
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Aatif
  • 11
  • 2
  • I don't think this question is a duplicate. The linked question deals with single-character delimiters (like "[" and "]") and that solution won't work with the multi-character delimiters in this question (like "->" and "<-") so it makes sense for this to stay open as its own question. – Sly_cardinal Nov 08 '18 at 08:12

2 Answers2

2

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);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

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)
Sly_cardinal
  • 12,270
  • 5
  • 49
  • 50