Javascript - regex - word boundary (\b) issue

前端 未结 3 1135
悲哀的现实
悲哀的现实 2020-12-01 16:00

I have a difficulty using \\b and greek characters in a regex.

At this example [a-zA-ZΆΈ-ώἀ-ῼ]* succeeds to mark all the words I want (both

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 16:55

    Since Javascript doesn't have the lookbehind feature and since word boundaries work only with members of the \w character class, the only way is to use groups (and capturing groups if you want to make a replacement):

    (?m)(^|[^a-zA-ZΆΈ-ώἀ-ῼ\n])([a-zA-ZΆΈ-ώἀ-ῼ]{2})(?![a-zA-ZΆΈ-ώἀ-ῼ])
    

    example to remove 2 letters words:

    txt = txt.replace(/(^|[^a-zA-ZΆΈ-ώἀ-ῼ\n])([a-zA-ZΆΈ-ώἀ-ῼ]{2})(?![a-zA-ZΆΈ-ώἀ-ῼ])/gm, '\1');
    

提交回复
热议问题