How "regexp[foo OR bar]"

55 views
Skip to first unread message

Mat

unread,
Jul 1, 2020, 6:16:26 AM7/1/20
to TiddlyWiki
I wish to ensure that a string contains the string /> OR </$ (or both)

and I have those two strings in a single field called "widgetclosure" i.e

widgetclosure: /> </$

How can I formulate a regexp to ensure some string includes any of those? i.e

<$set name=closures filter="[{!!widgetclosure}escaperegexp[]]"
{{{ mystring +[regexp
<closures>] }}}

I do not want to use additional fields but it would be fine to modify the field value to, for example, read:

widgetclosure: />|</$

Ideally the field can contain an arbitrary number of strings (again, possibly separated via some "regexp" character to make it work)

Thank you!

<:-)

Mat

unread,
Jul 1, 2020, 7:20:18 AM7/1/20
to tiddl...@googlegroups.com
Edit: Nope, not solved here. Skip this post and see the ones after instead.
________________________________________________________

Solved it. To my surprise, no escaperegexp is needed, i.e:

<$set name=closures filter="[{!!widgetclosure}]"
{{{ mystring +[regexp
<closures>] }}}

...is enough when widgetclosure: />|</$

No need to escape e.g the / characters. Convenient (...but how can this possibly work?)

<:-)

TiddlyTweeter

unread,
Jul 1, 2020, 7:54:41 AM7/1/20
to TiddlyWiki
Mat is that matching the "</$" cases? ... 

In regex in TW "/" is not an issue. It does not need escaping. It's only an issue in raw JavaScript.

Just FYI my take on your issue is ...

(/>|</\$)+

notes: the "\" before $ suppresses the special meaning of $ in regex. The "+" at the end forces the alternation to repeat if necessary (I.e. makes it or/and).
The pattern assumes there would be no spaces between ">" and "<".

Best wishes
TT

Mat

unread,
Jul 1, 2020, 8:18:47 AM7/1/20
to TiddlyWiki
@TiddlyTweeter, 

I did find that it was not solved previously after all. Basically to not use escaperegexp is insufficient - but using escaperegexp puts escapecharacters on / characters where they should not be which also doesn't work! I think the escaperexep operator needs to be modified but I'm not sure how to formulate it.

Anyway, only thanks to your post (THANK YOU!) do I now have a filter that does it:

The field closures contains: /> </$

<$set name=esc
  value={{{ [enlist{!!closures}escaperegexp[]join[|]addprefix[(]addsuffix[)+]split[\/]join[/]] }}}
>
 {{{ mystring +[regexp
<esc>] }}}
</$set>

mystring can now contain either of /> or </$ and be accepted by the regexp operator.

Again, thank you TT!

<:-)

TiddlyTweeter

unread,
Jul 1, 2020, 8:49:22 AM7/1/20
to TiddlyWiki
Mat, where you set a *regex* in a field or variable one should just use the full standard regex notation, including regex escapes when needed I think. If you run escaperegex over those it will mess them up in a potentially confusing fashion.

As far as I understand it the escaperegexp is for situations like titles where you want to suppress regex behaviour so they can be processed as pure text. Something like that.

My 2 cents
TT
Reply all
Reply to author
Forward
0 new messages