I am working on a regexp matcher to find a string in a substring. This string happens to be, in my use-case, a class name that comes after the html attribute class=
. While it may appear a duplicate of the historic answer in the question "regex-match-open-tags-except-xhtml-self-contained-tags", I think it's more like "finding instance of a string inside a larger string", and is what regular expressions can help with, in my understanding...but then again, here I am at Stack Overflow.
I thought word boundaries might do what I want, as it works with ruby gsub
to find the occurences of my search string within a larger string, but it isn't returning the correct results when using a regexp matcher. I think I am reaching the limits of word boundary anyways, when trying to match text that follows an occurence of class=
.
I'm wondering if there's other things in the regexp toolbox to use to achieve the following:
When searching for string "text-small" I want to one occurrence to be returned for the following cases:
- class="text-small"
- class="other-class text-small"
- class="text-small other class"
So we can notice a few requirements: match a space before, or match a space after the string, or match a " before or after the string, and the string needs to be preceded at some point by a class=
.
We do not want to match class="text-small-v2" and do not want to match random occurrences of "text-small" without being inside a class=
An example:
So <p class="text-small some-other-class">Hey here is some text-small too</p><span class="text-small-v2">hello there</span>
should result in 1 occurrence.
Here's the rubular permalink using @bobblebubble 's suggestion, which seems to work well: https://rubular.com/r/yXhEnbxp6OKtZk
Thanks for any and all help!