DEV Community

Discussion on: React.js - Interview Question - duplicate hashtag remover.

Collapse
 
rajeshroyal profile image
Rajesh Royal

@rhobert72 nice solution, also can you please write a little bit about the regex expression you used to understand it better. Like what this line is doing txt.match(/\B#\S+\b/gm)

Thanks.

Collapse
 
rhobert72 profile image
Roberto Garella

To find a tag within a text we have to play with word boundaries.
With \B (b upper case) we are looking for a position is not preceded and not followed by a word character. In our case we are looking for a word starts with # precede by a white space.
Instead, with \b (notice b is lower case) we are looking for a position preceded by a word character but not followed by a word character.
With \S+ we are looking for all characters (one or more than one) excluded: new line, tab and so on.
Flags: g stands for global and tells to regexp engine don't stop at the first match; m stands for multiline.
Flag m is not necessary. 😁

Thread Thread
 
rajeshroyal profile image
Rajesh Royal

Thanks @rhobert72 ✌🏻