Ryszard Jędraszyk Ryszard Jędraszyk - 1 year ago 66
Javascript Question

translating RegEx syntax working in php and python to JS

I have this RegEx syntax: "(?<=[a-z])-(?=[a-z])"

It captures a dash between 2 lowercase letters. In example below the second dash is captured:

Krynica-Zdrój, ul. Uzdro-jowa

Unfortunately I can't use <= in JS.
My ultimate goal is to remove the hyphen with RegEx replace.

Answer Source

It seems to me you need to remove the hyphen in between lowercase letters.


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

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.


  • ([a-z]) - Group 1 capturing a lowercase ASCII letter
  • - - a hyphen
  • (?=[a-z]) - 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
  • "$1" - the replacement pattern containing just the backreference to the value stored in Group 1 buffer.

VBA sample code:

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

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download