开发者

Node.js Parsing A Number Inside a String

开发者 https://www.devze.com 2023-02-28 12:26 出处:网络
Given a string like: Recipient: test@test.com Action: failed Status: 5.0.0 (permanent failure) Diagnostic: No

Given a string like:

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

How do I g开发者_JAVA百科et the "5.0.0" and "permanent failure" only if it's always after Status: ? ?

Thanks


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号