Match ":)" emoticon followed by word boundary

I am trying to match emoticons followed by a \b word boundary.

Let's say I want to combine :p and :) , and then \b .

/(:p)\b/ works fine, but why does /(:\))\b/ behave the other way around?

+6
source share
2 answers

You cannot use the word boundary here because ) is a character other than the word.

Simply put: \b only allows words to be executed using a regular expression like \bword\b . A word symbol is a symbol that can be used to form words. All characters that are not characters of a word are characters without a word .

Use (:\)) to match :) and write it to the first capture group.

Use /(:\))(?![a-z0-9_])/i to avoid matching any :) with the letters after the emoticon. This is equivalent to (:\))\B

\b is a negative version of \b . \b matches at every position where \b not. Effectively, \b matches any position between two words of characters, as well as at any position between two characters other than a word.

See demo 1 and demo 2 .

+7
source

Addition to stribizhev answer .. you can use (:\))\B

Examples of using:

\b : string = That man is batman. regex = \bman\b matches only man , not man in batman , because the position between tm not a word boundary (this word).

\b : string = I am bat-man and he is super - man. regex = \B-\B matches - in super - man , while \B-\B matches - in bat-man , since the position between t- and -m is the word boundary .. and (space) - , - (space) not.

Note. . It's easy to see if you consider \b or \b as the position between two characters, and if the transition from character to character is equal to word to word or word to non word

+4
source

Source: https://habr.com/ru/post/986041/


All Articles