Matching tags with regular expressions

164 views
Skip to first unread message

Reto

unread,
Jan 5, 2012, 3:21:04 AM1/5/12
to TiddlyWiki
With forEachTidler I am trying to find tiddlers with tags matching
certain patterns. I read carefully through the posts in this group
related to this topic but wasn't able to figure out a solution.

This is what I have:

<<forEachTiddler where
'tiddler.tags.sort().join("]]").contains(new RegExp("....\-..
\-..","gm"))'
>>

Date tags with format "YYYY-0MM-0DD" should be found. As far as I
understood the tag array is converted to a string after the join but I
obviously do not understand how to apply the RegExp function to it ...

Any hints welcome!

Eric Shulman

unread,
Jan 5, 2012, 5:23:50 AM1/5/12
to TiddlyWiki
> <<forEachTiddler where
> 'tiddler.tags.sort().join("]]").contains(new RegExp("....\-..
> \-..","gm"))'
>
> Date tags with format "YYYY-0MM-0DD" should be found. As far as I
> understood the tag array is converted to a string after the join but I
> obviously do not understand how to apply the RegExp function to it ...

To test a text string against a regular expression, you can use
the .match() method of the string, passing the regexp as an argument:

var foo="some text";
if (foo.match(/some regexp/)) { ... }

If the pattern is found within the string, match() returns an *array*,
where match[0] is the complete matched text, and match[1..n] contain
matches for parenthesized sub-expressions (if any were used in the
regexp). If no match is found, then match() returns a NULL (==false)
value.

Try this:

<<forEachTiddler where
'tiddler.tags.join(" ").match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/)'

* I used a literal regexp (/.../) instead of creating a new RegExp
object within the forEachTiddler loop (more efficient).
* The regexp uses more specific matching (looks for numerics only)
* Except within the "[0-9]" regexp syntax, "-" is treated literally
and is not a special symbol. It doesn't need backslash-quoting to be
matched.
* The tags don't need to be sorted, since you are matching across the
whole set of tags at once
* The tags are joined with " " rather than "]]".

enjoy,
-e
Eric Shulman
TiddlyTools / ELS Design Studios

----
TiddlyTools needs YOUR financial support...
Help ME to continue to help YOU...
make a generous donation today:
http://www.TiddlyTools.com/#Donations

Professional TiddlyWiki Consulting Services...
Analysis, Design, and Custom Solutions:
http://www.TiddlyTools.com/#Contact

Reto

unread,
Jan 5, 2012, 6:55:25 AM1/5/12
to TiddlyWiki
Works like a charm, muchas gracias! And thanks for the helpfull
coments.

Cheers
Reto

Reto

unread,
Jan 7, 2012, 5:04:00 PM1/7/12
to tiddl...@googlegroups.com
Any idea how to avoid the NULL respectively replace it by an empty string if match doesn't get any result?

tiddler.tags.join(" ").match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/).replace(/\0/g,"")

ends up with "TypeError: Cannot  call method 'replace' of null".


Eric Shulman

unread,
Jan 7, 2012, 10:04:44 PM1/7/12
to TiddlyWiki


> Any idea how to avoid the NULL respectively replace it by an empty string
> if match doesn't get any result?
>
> tiddler.tags.join(" ").match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/).replace(/\0/g,"")

Use this:

tiddler.tags.join(" ").match(/[0-9]{4}-[0-9]{2}-[0-9]{2}/)||""

-e



Reto

unread,
Jan 9, 2012, 2:38:25 AM1/9/12
to tiddl...@googlegroups.com
Yes, that's it. Thanks!

Reto
Reply all
Reply to author
Forward
0 new messages