1

I'm trying to find a dash - from a string if it match certain conditions and only the dash should be selected by regex.

Cases it should be selected -

  1. If both sides has space. Example: test - dash
  2. If right hand side has space. Example: test- dash
  3. If both sides don't have space. Example: test-dash

Cases that it shouldn't be selected

  1. If there's no space on right side, but there's space on left side. Example: test -dash

Here's my progress

enter image description here

as the screenshot shows, I can achieve with positive-lookbehind, but this is not widely supported.

So, my question is, is there an alternative way to achieve this without using positive-lookbehind?

Thanks.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Jamen
  • 169
  • 8
  • 1
    Most of the many regex engines I dealt with have this feature. How you made the conclusion it is not widely supported? – Bing Wang Jan 22 '21 at 03:57
  • @BingWang I looked at caniuse, it says IE11 and safari doesn't support it unfortunately, and my app is targeting IE 11 – Jamen Jan 22 '21 at 04:11

3 Answers3

0

This should work:

\S+( ?)-(\1| )\S+

https://regex101.com/r/XI6W4R/1

It uses the first capture group to know whether there is a space before the dash. If there is, then there must be a space after the dash. Otherwise, the space after the dash is optional.

Backreferences are more widely supported than positive-lookbehinds (according to this Regular Expression Engine Comparison Chart).


I'm trying to ONLY have the - selected. instead of the whole thing

In this case my knowledge is limited to offering the use of the following regex

\S+( ?)(-)(\1| )\S+

and using capture group 2 to find the dash. It's also difficult to offer better advice without knowing the exact way in which this regex is being used.

https://regex101.com/r/XI6W4R/2

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
  • thanks for you help. it partially works, coz I'm trying to ONLY have the `-` selected. instead of the whole thing – Jamen Jan 22 '21 at 04:10
  • @Jamen apologies I didn't fully read the question. I'm not sure if this method can only select the dash because it relies on capturing the value of the character before. Would it work if the dash were to be captured in a group which you could use? Like `...(-)...`, and then use capture group 2 to locate the dash. – Callum Watkins Jan 22 '21 at 04:15
  • @Jamen in fact the more I think about it, the more I am convinced it is not possible to match _only_ the dash whilst evaluating the surrounding characters without lookarounds, even using tricks like `\K`. I am by no means an expert though so I'd be glad to be wrong. I would say try and use a capturing group to match the dash and go from there (see my edit). – Callum Watkins Jan 22 '21 at 04:26
  • right. I can only achieve that with the `lookbehind` trick but unfortunately one of the browsers that my app is targeting doesn't support this feature. And in fact, I don't have to have only the `-` selected, I'm just curious if there's a way to do it without `lookbehind`. anyway, again, thx for your help. – Jamen Jan 22 '21 at 04:37
0

You can use

-(?!(?<=\s-)\S)

See the regex demo.

Details:

  • - - a hyphen
  • (?!(?<=\s-)\S) - that is not immediately followed with a non-whitespace char (that is, there must be a space or end of string immediately on the right) that is, in its turn, immediately preceded with a whitespace and - char.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

For the example data, if you can not use a lookbehind perhaps it might suffice to use a word boundary.

-(?=\s)|\b-(?=[^\s-])

Explanation

  • -(?=\s) Match - and assert a whitespace char to the right
  • | Or
  • \b-(?=[^\s-]) Word boundary, match - and assert at the right a non whitespace char except -

Regex demo

The fourth bird
  • 154,723
  • 16
  • 55
  • 70