Update: use <cms:each> with is_regex parameter or <cms:validate> with case_sensitive parameter.

Code: Select all
<cms:validate string validator="regex=%<cms:show substr />%" case_sensitive='1' />



Hi people,
Sometimes we want to know if a word is present in a string. There is no such tag in CouchCMS like "cms:in", neither it is possible to write "cms:if 'word' in 'text' ". Posting a solution here.

A good solution is to validate with "cms:validate" and a RegEx validator.
Sample:
Code: Select all
<cms:validate 'London, Paris, Moscow, Rome' validator="regex=/Paris/i" />

Result: "Paris" is found among the list of cities.

Alternatively, we can use variables just as effectively.
Code: Select all
<cms:set string = 'London, Paris, Moscow, Rome' />
<cms:set substr = 'Paris' />

<cms:validate string validator="regex=%<cms:show substr />%i" />

The result of validation would be 1 or 0.

A caveat: at the moment Couch only supports case-insensitive matching.

To limit the spread of this annoying problem in certain cases we can refer to following sample:
Code: Select all
<cms:set string = 'Ukraine is a country where russians also live.' />
<cms:set substr = 'Russia' />

<cms:validate string validator="regex=%\b<cms:show substr />\b%i" />

Introducing a "\b" means a word boundary, so 'russia' will not be found, even if 'russians' are in text.

More of the cms:validate tag in this docs post viewtopic.php?f=5&t=8581&p=16352#p16352
More on available validators in regular docs
http://docs.couchcms.com/tags-reference/editable.html

PS. Right after publication of this post, @KK have come with a solution, that will be available in latest update.
Code: Select all
<cms:validate string validator="regex=%<cms:show substr />%" case_sensitive='1' />