Select a character if some character from a list is before the character

前端 未结 2 1735
心在旅途
心在旅途 2021-01-28 17:57

I have this regular expression:

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

This regex selects č- from my text:

s         


        
2条回答
  •  粉色の甜心
    2021-01-28 18:54

    In other languages you would use a 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.

提交回复
热议问题