Skip to content

Working with Text: Strings and Regular Expressions

Programming Node.js Difficulty 1/5

Beginning of the lesson

Working with Text: Strings and Regular Expressions

Slice it, search it, split it, and match it - become a text wizard in Node.

Big Idea: Almost everything a program touches is text: names people type, lines read from a file, JSON from an API, commands from the terminal. A string is how Node stores that text, and Node gives you a whole toolbox of string methods (plus a pattern language called regular expressions) to cut, clean, and search it.
By the end you will be able to:
  • measure a string and read single characters by their position,
  • slice a piece out of a string and search inside it,
  • change case, trim spaces, and split and join text,
  • build strings neatly with template literals,
  • replace text, and use a first regular expression to match and validate patterns.

1. What is a string?

A string is a piece of text: a word, a sentence, an emoji-free label, or even an empty ''. You write one by wrapping characters in single quotes, double quotes, or backticks:

const name = 'Ada';
const city = "Lovelace City";
const greeting = `Hello there`;

Analogy. Think of a string as a necklace of beads. Each bead is one character, and the beads are kept in a fixed order. You can count the beads, look at the third bead, or take a section of the necklace - but you cannot change one bead in place, because in JavaScript strings are immutable (read-only). Every "change" actually builds a brand-new string.

Strings live everywhere in the earlier topics of this course: the text you read with the fs module, the keys and values inside JSON, and the arguments you pull off process.argv in a CLI are all strings. Mastering strings makes all of those easier.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers