Node.js 解析字符串中的数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5764893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:04:29  来源:igfitidea点击:

Node.js Parsing A Number Inside a String

regexnode.jsserverside-javascript

提问by donald

Given a string like:

给定一个字符串,如:

Recipient: [email protected]
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: ? ?

仅当始终在 Status: 之后时,我如何才能获得“5.0.0”和“永久故障”??

Thanks

谢谢

回答by Máthé Endre-Botond

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-9\.], [a-zA-Z ] 选择器。现在第一个需要数字和点,第二个字符和空格