RegEx question (not directly TW, but using it for a plugin...)

29 views
Skip to first unread message

David Szego

unread,
Mar 21, 2017, 2:43:46 PM3/21/17
to TiddlyWikiDev
Hi all you brilliant people who I keep bothering with seemingly simple questions (after the fact, at least!)


I seem to have hit upon a corner case - or it's so easy, no one has ever asked!


I have a list of desired words I'd like to match in a block of text. However, I only want to match the first occurrence of each of these.


If my block of text reads:


glass shoes door doors window door glasses. glass's glass window windows glasses

I'd like to match:


glass door window

I'm learning how to narrow it down as I try to understand the groupings... This:


/(?:[\s]|^)door|glass|window(?=[\s]|$)/g;

gives me "ghost" occurrences:


glass, door, door,window, door,glass,glass,glass,window,glass

Wrapping my desired words in brackets as a set (group?) like this:


/(?:[\s]|^)(door|glass|window)(?=[\s]|$)/g;

gives me only every actual occurrence:


glass, door, window, door, glass, window

which is so far the best (narrowest) I can get.

I would have expected that:


/(door|glass|window)/

without the /g global flag would give me the first occurrence of "my" OR "desired" OR "words", but it stops at the first find, "glass".


So to restate the question, I'm trying to find the first instance of each of these words (first of door, and first of window, and first of glass) wrapped by (?:[\s]|^) and (?=[\s]|$), in a block of text.


Many thanks!

Reply all
Reply to author
Forward
0 new messages