How to detect if a string contains only whitespace (using regex)?

39 views
Skip to first unread message

Hubert

unread,
Dec 3, 2019, 4:51:03 AM12/3/19
to TiddlyWiki
Hello,

I'd like to detect if a string only consists of whitespace (i.e. is non-empty but has no characters). The filter should output an emptyMessage at all times except when the field data1 consists of whitespace and whitespace alone.

I have the following code with regex found online but it doesn't work. Would anyone have any idea how I could make this work?

<$set name="regex" value="/^\s*$/">
<$list filter="[{!!data1}regexp
<regex>]" emptyMessage="{{!!data1}}">

String contains only whitespace

</$list>
</$set>

Thank you,
Hubert

TiddlyTweeter

unread,
Dec 3, 2019, 5:48:51 AM12/3/19
to TiddlyWiki
Ciao Hubert

When you copy off net you need to discard the containing "/ ... /". You don't need them in TW regex operators. They are normally for handing "raw regex" in JS which TW itself handles for you.

Here is some code ... First matches !!field that is whitespace only. Second matches empty !!field. Third combines both. 

<$list filter="[{!!data1}regexp[^\s+$]]" emptyMessage="{{!!data1}}">

  Field is  whitespace

</$list>

<$list filter="[{!!data1}regexp[^$]]" emptyMessage="{{!!data1}}">

  Field is empty

</$list>

<$list filter="[{!!data1}regexp[^\s*$]]" emptyMessage="{{!!data1}}">

  Field is empty or is whitespace

</$list>

Best wishes
TT

Hubert

unread,
Dec 3, 2019, 6:16:32 AM12/3/19
to TiddlyWiki
Thank you TT! It works like a charm and solved my accidental empty input issue. I've saved your code for future use as well.

Thanks again!

Regards,
Hubert
Reply all
Reply to author
Forward
0 new messages