Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Writing better Alexa skills with text variations

Skills may appear boring if they use the same utterances over and over again. Quite a number of solutions have been described in the past to bring variation into responses.

The following text is intended to share my approach with you.

Say hello to "unwrap()"

This is a piece of code I use in almost every skill I write:

function unwrap(text) {
    var matches, options, random;
    var regEx = new RegExp(/{([^{}]+?)}/);
    while ((matches = regEx.exec(text)) !== null) {
        options = matches[1].split("|");
        random = Math.floor(Math.random() * options.length);
        text = text.replace(matches[0], options[random]);
    return text;

What does it do?

The function "unwrap" randomly "unwraps" text variations defined within curly braces ("{}") separated by "|" characters.

How to use it?

See the following examples to get an idea for what you can do with "unwrap".

  1. Randomly choose between one of two given options:

     let randomSpeech = unwrap("{Hello|Hi}");
  2. You are not limited to two variants:

     let randomSpeech = unwrap("{Hello|Hi|Cheerio|How do you do?|Welcome|Nice to have you here}");
  3. You can have multiple expressions within a string description:

     let randomSpeech = unwrap("{Hello|Hi} {my friend|again}");
  4. One option can be left empty to make parts of the text truly optional:

     let randomSpeech = unwrap("{Hello|Hi} {|my friend}");
  5. To make things more interesting expressions can be nested multiple times:

     let randomSpeech = unwrap("{Hello|Hi} {|my {|best} friend }");

    Especially this feature makes "unwrap" a very powerful tool.

  6. Homework: Can you count how many different possible utterances are descibed within the following expression?

     let randomErrorResponseSpeech = unwrap("{{|I am{|so}} sorry {|about { that|this}}!  {Something went {|totally} wrong|this did not work{|as expected}}");


Using this technique you have a very compact notation to define a lot of variations for your responses as a one liner in your code.


Unwrap text Variations






No releases published


No packages published