简体   繁体   English

将 php 和 python 中的 RegEx 语法转换为 JS

[英]translating RegEx syntax working in php and python to JS

I have this RegEx syntax: "(?<=[az])-(?=[az])"我有这个正则表达式语法:“(?<=[az])-(?=[az])”

It captures a dash between 2 lowercase letters.它捕获两个小写字母之间的破折号。 In example below the second dash is captured:在下面的示例中,捕获了第二个破折号:

Krynica-Zdrój, ul. Krynica-Zdrój, ul. Uzdro-jowa乌兹卓乔瓦

Unfortunately I can't use <= in JS.不幸的是,我不能在 JS 中使用 <=。 My ultimate goal is to remove the hyphen with RegEx replace.我的最终目标是用 RegEx 替换删除连字符。

It seems to me you need to remove the hyphen in between lowercase letters.在我看来,您需要删除小写字母之间的连字符。

Use

 var s = "Krynica-Zdrój, ul. Uzdro-jowa"; var res = s.replace(/([az])-(?=[az])/g, "$1"); console.log(res);

Note the first lookbehind is turned into a simple capturing group and the second lookahead is OK to use since - potentially, if there are chunks of hyphenated single lowercase letters - it will be able to deal with overlapping matches.请注意,第一个lookbehind 变成了一个简单的捕获组,第二个lookahead 可以使用,因为 - 可能,如果有大块的带连字符的单个小写字母 - 它将能够处理重叠匹配。

Details :详情

  • ([az]) - Group 1 capturing a lowercase ASCII letter ([az]) - 第 1 组捕获小写 ASCII 字母
  • - - a hyphen - - 一个连字符
  • (?=[az]) - that is followed with a lowercase ASCII letter that is not added to the result - /g - a global modifier, search for all occurrences of the pattern (?=[az]) - 后面跟着一个没有添加到结果中的小写 ASCII 字母 - /g - 一个全局修饰符,搜索所有出现的模式
  • "$1" - the replacement pattern containing just the backreference to the value stored in Group 1 buffer. "$1" - 仅包含对存储在 Group 1 缓冲区中的值的反向引用的替换模式。

VBA sample code : VBA 示例代码

Sub RemoveExtraHyphens()
Dim s As String
Dim reg As New regexp

reg.pattern = "([a-z])-(?=[a-z])"
reg.Global = True

s = "Krynica-Zdroj, ul. Uzdro-jowa"
Debug.Print reg.Replace(s, "$1")
End Sub

在此处输入图片说明

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

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