Module 4 Lesson 2: String Manipulation
·Automation

Module 4 Lesson 2: String Manipulation

Master the text. Learn how to uppercase, lowercase, trim, and use Regular Expressions (Regex) to extract specific information from strings like emails or phone numbers.

Module 4 Lesson 2: String Manipulation

Automation often involves "Cleaning" data. Maybe a user typed their name in ALL CAPS, or they added extra spaces to their email address.

1. Basic Javascript Methods

Since n8n expressions are Javascript, you can use standard methods:

  • tolowerCase(): "SUDEEP".toLowerCase() -> "sudeep"
  • trim(): " hello ".trim() -> "hello"
  • split(): "apple,banana".split(",") -> ["apple", "banana"]

2. Regular Expressions (Regex)

Regex is a "Pattern" used to find text.

  • Extracting a Domain: {{ $json.email.match(/@(.+)$/)[1] }}
  • Extracting a Number from text: {{ $json.msg.match(/\d+/)[0] }}
  • Scenario: Someone sends you an email saying "Order #12345 is late." You use Regex to extract just "12345."

3. The "Replace" Node

n8n has a dedicated "HTML Extract" and string manipulation logic. But for simple tasks, you can do it inside an expression for any field. $json.msg.replace("oldWord", "newWord")


4. Why String Cleaning Matters

If you send " sdevkota@gmail.com" (with a space) to an API, the API might return an error. By Trimming your strings in the "Set" node, you make your automation more resilient to human error.


Exercise: The Text Cleanup

  1. Write an expression that takes the string "John Doe" and returns "DOE, JOHN".
  2. Write a Regex to check if a string contains the word "Urgent" (Case-insensitive).
  3. If you have the string "Invoice-2023-OCT-01", how would you extract just the year?
  4. Research: What is the "Fixed" property in a Set node?
  5. Why is .split().pop() a common trick for getting the "Last part" of a URL?

Summary

String manipulation is the "Polish" of your automation. By learning how to transform and extract text using Javascript and Regex, you ensure that the data you send to your CRM or your database is clean, consistent, and professional.

Next Lesson: Calculators ready: Doing Math: The Math and Set Nodes.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn