Ad

Select A Character If Some Character From A List Is Before The Character

- 1 answer

I have this regular expression:

/([a-záäéěíýóôöúüůĺľŕřčšťžňď])-$\s*/gmi

This regex selects č- from my text:

sme! a Želiezovce 2015: Spoloíč-
ne pre Európu. Oslávili aj 940.

But I want to select only - (without č) (if some character from the list [a-záäéěíýóôöúüůĺľŕřčšťžňď] is before the -).

Ad

Answer

In other languages you would use a lookbehi//www.regular-expressions.info/lookaround.html" >lookbehind

/(?<=[a-záäéěíýóôöúüůĺľŕřčšťžňď])-$\s*/gmi

This matches -$\s* only if it's preceded by one of the characters in the list.

However, Javascript doesn't have lookbehind, so the workaround is to use a capturing group for the part of the regular expression after it.

var match = /[a-záäéěíýóôöúüůĺľŕřčšťžňď](-$\s*)/gmi.match(string);

When you use this, match[1] will contain the part of the string beginning with the hyphen.

Ad
source: stackoverflow.com
Ad