[英]using regex to split a string so It will return both whole and decimal numbers
I am in the middle of a project where in the beginning I had the following string <=10 & <20 and I want to split this string so it gives the following results ('<=' '10' '<' '20') the & is irrelevant because I deal with that in a different area of my code! 我在一个项目的中间,开始时我有以下字符串<= 10&<20,我想拆分此字符串,以便得到以下结果('<=''10''<''20' )&是无关紧要的,因为我在代码的不同区域中处理了它!
this is the solution I have when it is whole numbers 这是整数时的解决方案
.match(/(([<=>]+)|(\d+))/ig) - result: '<=' '10' '<' '20'
But the string can also have negative numbers <=1.2 & <2.2 and the results required from this would be ('<=' '1.2' '<' '2.2'). 但是该字符串也可以具有负数<= 1.2&<2.2,并且由此所需的结果将是('<=''1.2''<''2.2')。 So how would I update the .match above to handle both scenarios.
因此,我将如何更新上述.match来处理这两种情况。
To handle the dot, use a class containing both the digits and the dot : [\\d.]
. 要处理点,请使用包含数字和点的类:
[\\d.]
。 You may also remove the superfluous parenthesis : 您也可以删除多余的括号:
/([<=>]+)|([\d.]+)/ig
Example : 范例:
">= 2.2 & < 3.3".match(/([<=>]+)|([\d.]+)/ig)
gives 给
[">=", "2.2", "<", "3.3"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.