Our first real Node/Javascript example in Dynamic Web was a Valentine’s Day card. There is an empty form, and once it’s filled out, a new card is created. My tweak of this code involved a telegram. I thought it would be cute to make the form dynamically change the “.” to “-STOP-” like an old-timey telegram. Making this alteration turned out to be my first foray into the world of regex (regular expressions). Regex is necessary for matching strings of text. The period needs to be defined as a regex to distinguish it from the dots used to indicate segments of Javascript functions. It tells the code to search for text instead of code.

This is the replacement function:

newCard.message = newCard.message.replace(/./g,"-STOP-")

Read the rest of the code on Github.

Play with the Telegram app!

Here are some screenshots. This is the empty form:

This is the form filled out:

And here is the result: