15

Given a string like:

Recipient: test@test.com
Action: failed
Status: 5.0.0 (permanent failure)
Diagnostic: No

How do I get the "5.0.0" and "permanent failure" only if it's always after Status: ? ?

Thanks

donald
  • 23,587
  • 42
  • 142
  • 223

1 Answers1

36
var regex = /Status: ([0-9\.]+) \(([a-zA-Z ]+)\)/
var result = string.match(regex);
var statusNumber = result[1];
var statusString = result[2];

You should extend these: [0-9\.], [a-zA-Z ] selectors if you expect other characters in these values. For now the first one expects numbers and dots, the second characters and spaces

Máthé Endre-Botond
  • 4,826
  • 2
  • 29
  • 48
  • Why is this one regEx returning 2 results? – haemse Jul 14 '17 at 13:59
  • Alsoe ^ and $ should not be there – haemse Jul 14 '17 at 14:06
  • 1
    @haemse It's returning two results because there are two capturing groups in it. Actually it's returning three, the first one (at index 0) being the full match. The ^ and $ wasn't in the original answer. I removed them. Thanks. – Máthé Endre-Botond Jul 16 '17 at 09:13
  • I'm sorrry seems like str.match(reg) returns an array of all matches whereas reg.exec returns an array with details for one result. Also all the matching groups. – haemse Jul 16 '17 at 11:07