1

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.

3
  • I want to use replace to get rid of this hyphen. Commented Sep 22, 2016 at 13:04
  • About /\b-\b/g, it returns both hyphens. About the second pattern, I have no idea how to get it to work, without changes it doesn't detect anything. Commented Sep 22, 2016 at 13:16
  • Yes, I provided a solution below. Let me delete my first comment. Commented Sep 22, 2016 at 13:16

1 Answer 1

1

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(/([a-z])-(?=[a-z])/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.

Details:

  • ([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

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried /([a-z])-(?=[a-z])/g and it returns "o-" in my example.
Where is your example? Please share. If you use mine, you will see it works well.
My example is Krynica-Zdrój, ul. Uzdro-jowa, I have checked /([a-z])-(?=[a-z])/g on regex101.com/#javascript, I also maybe don't understand something, so I'll explain that I need the pattern to be used in VBA which uses JS RegEx syntax.
Thank You, now everything is clear to me and works perfectly, just replaced Dim reg As New regexp with Dim reg As Object Set reg = CreateObject("vbscript.regexp"), because I was getting error, probably had no reference to regexp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.