如何将Regex的lookbehind PHP结构替换为Javascript?

时间:2022-02-04 16:49:32

I have built Regex which is working properly with php , but not working in Javascript due to Invalid group structure error.

我已经构建了正确使用PHP的Regex,但由于无效的组结构错误而无法在Javascript中工作。

I know it is because Javascript does not support Lookbehind expressions. I tried to convert it but not able to do so.

我知道这是因为Javascript不支持Lookbehind表达式。我试图转换它但不能这样做。

Regex link : https://regex101.com/r/bC4hN2/1

正则表达式链接:https://regex101.com/r/bC4hN2/1

Regex Expression : (?<=^| )(.\d+)?(?=$| )|(?<=^| ).\d+(?=$| )

正则表达式:(?<= ^ |)(。\ d +)?(?= $ |)|(?<= ^ |)。\ d +(?= $ |)

Acceptance Criteria : Values should lie between 0 and 1 and should start with decimal point

验收标准:值应介于0和1之间,并应以小数点开头

Valid entries

  • .20
  • 0
  • 1
  • .1

Invalid Entries

  • 1.20
  • 1.0
  • 0.20
  • 2

This scenarios working properly with PHP Regex Expression , but not with Javascript due to errors.

此方案适用于PHP Regex Expression,但由于错误而无法使用Javascript。

How should i convert it to Javascript OR any alternate expression to achieve my scenario ?

我应该如何将其转换为Javascript或任何替代表达式来实现我的方案?

Thanks

1 个解决方案

#1


0  

This can be done easily as mentioned in comments but let's modify your regex. (Your match is in first capturing group)

这可以像评论中提到的那样轻松完成,但让我们修改你的正则表达式。 (您的比赛是在第一个捕获组)

(?:\s|^)([01]\b|(?:\.\d+))(?=\s|$)

Regex Demo

#1


0  

This can be done easily as mentioned in comments but let's modify your regex. (Your match is in first capturing group)

这可以像评论中提到的那样轻松完成,但让我们修改你的正则表达式。 (您的比赛是在第一个捕获组)

(?:\s|^)([01]\b|(?:\.\d+))(?=\s|$)

Regex Demo