简体   繁体   English

正则表达式匹配字符串的开始字符+空格,然后是数字,如货币

[英]regular expression to match start character of string + space then digits like currency

How do I test a string that contains a character at the start then a space and whatever comes after that which could be an integer or float?如何测试一个字符串,该字符串在开头包含一个字符,然后是一个空格,然后是 integer 或浮点数?

Example:例子:

'$ 15' '$ 15'

'$ 15.95' '$ 15.95'

/\$\s[\d]*\.[\d]{2}|\$\s[\d]*[^.]/gm

$space(endless digits until)period and exactly-2digits
|OR
$space(endless digits-no periods)

 const rgx = /\$\s[\d]*\.[\d]{2}|\$\s[\d]*[^.]/gm; const str = `\$ 15.95 \$ 5 \$ 2.00 \$ 6.90 \$ 1101`; let matches = [...str.matchAll(rgx)].flat(); console.log(matches);

I think it could help you: ^\$ [+-]?[0-9]{1,}(\.[0-9]{1,}){0,1} , you can test it here: https://regex101.com/我认为它可以帮助你: ^\$ [+-]?[0-9]{1,}(\.[0-9]{1,}){0,1} ,你可以在这里测试它: https ://regex101.com/

  • ^\$ : start with a character ^\$ : 以字符开头
  • then add a space然后加一个空格
  • [+-]? then add optional signs然后添加可选标志
  • [0-9]{1,} then add at less one digit [0-9]{1,}然后添加少一位数
  • (\.[0-9]{1,}){0,1} then add a dot and at less one digit (\.[0-9]{1,}){0,1}然后添加一个点并且少于一位

You can use below regular expression:您可以使用以下正则表达式:

. . \d+(?:.\d+)? \d+(?:.\d+)?

Test it here: https://regex101.com/r/1oqIO8/1在这里测试: https://regex101.com/r/1oqIO8/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM