Validating clipboard contents with contains command

I’m sure I’m missing something super simple here.

I’m trying to validate that the clipboard contents are correct user token. A correct token begins with "B$_".

This is what I’ve been attempting:

{token={clipboard}}

{if: contains(token, "B$_(.+)")}
{clipboard}
{else}Oh no! It looks like the token is not copied to your clipboard properly.
{endif}

Hi Peter,

The "contains()" function only works with plain text matches, it doesn't support regular expression syntax.

You want to use the "testregex()" function. Also you need to escape the "$" in the regex by putting a backslash before it as "$" has a special meaning in regular expressions, signifying the "end of the text".

Here's an example:

{token="B$_ABC123"}

{if: testregex(token, "B\$_(.+)")}
{=token}
{else}Oh no! It looks like the token isn't valid.
{endif}

Aha! that works perfectly! Thanks Scott!

Heya Scott, I’ve been using this bit of code for a while and have run into a situation where I need to pass the resulting value on to use in other parts of the snippet. Is it possible to pass the result of this if statement into a urlload command?

Essentially filtering the contents of the clipboard to verify that it begins with B$_ and if not add it to the URL in the urlload.

Yes, that should be possible. Just include the {=token} in the URL you want to load.

Let me know if you run into any issues.

Aha, my apologies! I forgot I modified the example to consider if the clipboard contents contain B$_ or not and then add it if not. Currently this is what I have.

{if: testregex({clipboard}, "B$(.+)")}
{=trim({clipboard})}
{else}
B$
{=trim({clipboard})}{endif}

Ideally if the token is copied without the B$_ I’d like to add it and insert that result into a urlload.

Sure, you should be able to do something like this:

{if: testregex({clipboard}, "B$(.+)")}
{token=trim({clipboard})}
{else}
{token= "B$" & trim({clipboard})}{endif}

{urlload: https://example.com/{=token}}

This works perfect! Thank you so much!