GROQ: How to match exact string subset and exclude partial matches (WW not UWW)?

15 replies
Last updated: Dec 15, 2020
Hello guys, need some help with GROQ. How do you go about matching only a subset of string in a given word?
Say I have data like:


[
  { id: 1, item: "SPTL65-UWW-13" },
  { id: 2, item: "HDTL65-WW" },
  { id: 3, item: "HDTL65-UWW" }
]
My goal is to return only the second item which has specific
WW
and NOT
UWW
.
I have come up with this query:


*[item match "-WW*"]
but this returns no match
*[item match "WW*"]
returns the 3 items though
AI Update

The match operator in GROQ isn't the right tool for your use case. The match operator is designed for full-text search with tokenization, not exact string pattern matching. When you use match, special characters like hyphens (-) are treated as word separators, which is why your query isn't working as expected.

For matching exact substrings or patterns with punctuation, you should use GROQ string functions instead. Here are a few approaches:

Since you want to match -WW but not -UWW, you can split on the hyphen and check the last segment:

*[array::compact(string::split(item, "-"))[-1] == "WW"]

This splits "HDTL65-WW" into ["HDTL65", "WW"] and checks if the last element equals "WW".

Solution 2: Check for exact suffix pattern

You could filter by checking if the item ends with the exact pattern you want:

*[item match "WW" && !(item match "UWW")]

However, this still relies on match's tokenization behavior and might not be reliable for all edge cases with punctuation.

Solution 3: More explicit string checking

A more explicit approach using string functions:

*[
  item match "WW" && 
  !string::startsWith(array::compact(string::split(item, "-"))[-1], "U")
]

Why match doesn't work here

As explained in the match operator documentation, match tokenizes text by breaking it on word boundaries and special characters. So "HDTL65-WW" gets indexed as ['HDTL65', 'WW'], and "HDTL65-UWW" becomes ['HDTL65', 'UWW'].

When you use *[item match "-WW*"], the hyphen is treated as a separator (not part of the search term), so the query essentially becomes meaningless. And *[item match "WW*"] matches all three documents because they all contain tokens that start with or match "WW" (since "UWW" contains "WW" as part of the token).

For exact string matching with punctuation, always prefer the string namespace functions like string::split(), string::startsWith(), or combine them with array functions for more complex patterns.

Show original thread
15 replies
Hey there! 🙂 Maybe this works for you?

*[item match "*-WW-*"]
Alternatively, if WW and UWW are the only options, you could also try this:
*[!(item match "*-UWW-*")]
Thanks. I’ll try your suggestions.
Thanks
user A
, your suggestion works.
user M
while yours also works, there are other options than those items.
Guys, appreciate the help. Thank you so much
You're welcome 😀 Great to hear that it works!
Hello guys
user A
and
user M
, I’m encountering some problems, unfortunately there are oddities with “match” on array values.
Even if equality
==
doesn’t work 😔
Upon further playing with this, I guess it works as expected. However, I guess what I’m really trying to achieve is to remove the other items in
productConfigurator
that aren’t matching the query above
I think I understand what you need 🙂 One way to do it could be to add the same filter when selecting which attributes you need returned. I played a bit around with the arcade you sent and I think this should do the trick.
However there might be some hidden fancy GROQ feature I don't know of which does this better.

It does remove all the non-matching items from the array though. Hope it helps!
Oh, I could cry right now
user A
. Thank you so much. 🙏
That’s it. Thank you so much. You save me today! God bless. huhu
You're very welcome 😁 Glad to hear it helps!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?