Regex to find whitespace at beginning or end of string

When users enter their name in the chat, I would like to make sure they don’t enter a whitespace at the beginning or the end. It is ok to have a whitespace between words (in case they enter two words, such as first and last name), but they shouldn’t enter a whitespace at the beginning or the end. I searched for a regex that would work, but most of them use a dollar sign as end of string anchors (such as \s$). The problem is that MobileCoach doesn’t seem to let me use dollar signs for this purpose. To avoid the dollar sign, I tried \S+\s?\S+ which works on https://www.freeformatter.com/java-regex-tester.html, but not in my intervention. Any suggestions?

Dear Sonja,

Based on our interventions, participants usually do not add any whitespace (neither at the beginning nor at the end, in the middle, it does not matter) when we ask them. For the very few exceptions, the intervention will not break. So, technically, it is not a problem to have whitespace in the string variables (e.g. $participantName). However, if you like, you can create a Decision Point in MC Designer and use a simple JavaScript code to trim the whitespace at the beginning and at the end of a string variable.

Here is an example:

  1. Set up conversational turns like this one here:

  2. In the Decision Point “checking for correct participant name”


    add a new rule and set the comparator to “execute javascript in x and store values but result is always true”. Finally, add the following code into the field “Rule [x]” text field:

     var x = {"participantName" : "$participantName".trim()};
     x;
    

This should then result in the following dialog.

I hope this helps. Best, Tobi

Dear Tobi, many thanks for the example and explanations. Unfortunately, the solution doesn’t seem to work yet. Maybe you can spot the mistake in my intervention?

  1. Conversational turns with decision point:

  2. Added the new rule in the decision point:
    image

  3. Details of the new rule:

  4. Details of “Rule [x] (with placeholders)”:
    image

  5. Example in the dialogue (still with whitespaces after the decision point):

Thanks again,
Sonja

It seems that your quotation marks are the wrong ones. Please use these here

"

, i.e. for both "participantName" and "$participantName".

Best, Tobi