IntermediateLesson 9 of 16
Strings, Regex & JSON – JavaScript
Processing text, searching for patterns and exchanging data.
Strings are immutable – every method returns a new string. The most important helpers:
const s = " JavaScript is fun ";
console.log(s.trim().toLowerCase());
console.log("a,b,c".split(","), ["x", "y", "z"].join("-"));
console.log("JavaScript".replace("a", "A"), "JavaScript".replaceAll("a", "A"));
console.log("abc".startsWith("a"), "abc".includes("d"), "abc".at(-1));
console.log("7".padStart(3, "0"), "ab".repeat(3), "JavaScript".slice(0, 4));
console.log([..."Python"].reverse().join("")); // nohtyPWith regular expressions you find and replace patterns. The g flag means "all matches", and named groups make results readable:
const text = "Contact: ada@example.com, bob@test.org";
console.log(text.match(/[\w.]+@[\w.]+\.\w+/g));
console.log("Phone 12345".replace(/\d+/g, "#"));
const m = "2026-03-15".match(/(?<jahr>\d{4})-(?<monat>\d{2})-(?<tag>\d{2})/);
console.log(m.groups.tag + "." + m.groups.monat + "." + m.groups.jahr); // 15.03.2026
console.log(/^\d+$/.test("12345"), /^\d+$/.test("12a45")); // true falseJSON is the standard format for data exchange on the web. JSON.stringify produces text, JSON.parse reads it back in:
const daten = { name: "Ada", sprachen: ["JavaScript", "C"], aktiv: true };
const json = JSON.stringify(daten, null, 2); // 2 spaces of indentation
console.log(json);
const zurueck = JSON.parse(json);
console.log(zurueck.sprachen[0]);
console.log(JSON.stringify({ a: 1, b: undefined, c: () => 1, d: new Date(0) }));
// {"a":1,"d":"1970-01-01T00:00:00.000Z"}Warning
JSON.parse throws a SyntaxError on invalid text – catch it when the data comes from outside. undefined, functions and symbols disappear during serialization, and dates become strings.TipFor German-style number and date formats:
(1234.5).toLocaleString("de-DE") returns 1.234,5, and new Intl.DateTimeFormat("de-DE").format(datum) returns a date like 15.3.2026.