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

前端 未结 2 1733
心在旅途
心在旅途 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:39

    First, in regex everything you put in parenthesis will be broken down in the matching process, so that the matches array will contain the full matching string at it's 0 position, followed by all of the regex's parenthesis from left to right.

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

    Would have returned the following matches for you string: ["č-", "-"] so you can extract the specific data you need from your match.

    Also, the $ character indicates in regex the end of the line and you are using the multiline flag, so technically this part \s* is just being ignored as nothing can appear in a line after the end of it.

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

提交回复
热议问题