2

I have a string like this ...

var str = "6 validation errors detected: Value '' at 'confirmationCode' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value '' at 'confirmationCode' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'password' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value at 'password' failed to satisfy constraint: Member must have length greater than or equal to 6; Value at 'username' failed to satisfy constraint: Member must satisfy regular expression pattern: [\p{L}\p{M}\p{S}\p{N}\p{P}]+; Value at 'username' failed to satisfy constraint: Member must have length greater than or equal to 1";

I want to extract all the "unique" lines that start with the word ... "Value" ...

So expected output is ...

  • Value '' at 'confirmationCode' failed to satisfy constraint
  • Value at 'password' failed to satisfy constraint
  • Value at 'username' failed to satisfy constraint

Here is what I have tried so far ...

var x = str.split("\;|\:")  // This is NOT working
console.log(x);

var z = y.filter(word => word.indexOf("Value") > -1) // Also this needs to be tweaked to filter unique values
console.log(z);

Performance is an issue, so I prefer the most optimized solution.

1 Answer 1

3

You might use a single regular expression, no split or filter or loops or other tests needed:

var str = "6 validation errors detected: Value '' at 'confirmationCode' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value '' at 'confirmationCode' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'password' failed to satisfy constraint: Member must satisfy regular expression pattern: [\S]+; Value at 'password' failed to satisfy constraint: Member must have length greater than or equal to 6; Value at 'username' failed to satisfy constraint: Member must satisfy regular expression pattern: [\p{L}\p{M}\p{S}\p{N}\p{P}]+; Value at 'username' failed to satisfy constraint: Member must have length greater than or equal to 1";

console.log(
  str.match(/((^|Value)[^:]+)(?!.*\1)/g)
);

Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to also get the opening line "6 validation errors detected" along with the "value" lines ?
See edit, just match the beginning of the string or Value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.