我正在尝试使用正则表达式匹配函数将以下字符串拆分为子字符串。但它并没有分割最后一个元素。谁能指出我犯的错误吗?
"<=lastdate+30d".match(/(>=|<=|==|<|>|\!=)|\w+|[-+]|\d|[dhms]/g)
result is ["<=", "lastdate", "+", "30d"]
expected result is ["<=", "lastdate", "+", "30", "d"]
最佳答案
您的正则表达式的问题是 \w
与 [a-zA-Z0-9_]
匹配。因此,最后一个 30d
与 \w+
匹配。
使用下面的正则表达式:
/(>=|<=|==|<|>|\!=)|[a-z]+|[-+]|\d+|[dhms]/
console.log("<=lastdate+30d".match(/(>=|<=|==|<|>|\!=)|[a-z]+|[-+]|\d+|[dhms]/gi));
关于javascript - 用于将 <=last date+30d 拆分为 5 个数组的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50712487/