Level: Intermediate | Player: 07_Arrangement_Basics
Learning Objectives
- Strings can be assembled using template literals.
- Know frequently used methods such as
split,join,replace,includes, andtrim. - Understand that strings are immutable.
- Know the basic grammar of regular expressions (literals, flags) and be able to use To-search/replace.
- Access characters with index and
length.
1. Template literal
Supports backticks (`), you can wrap it with a line break and${expr} interpolation.
const name = "Sua";
const greet = `Hello, ${name}!
Fighting today too’;
It has much better readability than + for string concatenation.
2. Frequently used methods
All string methods return a new string without replacing the original.
" hi ".trim(); // "hi"
"hello".toUpperCase(); // "HELLO"
"a,b,c".split(","); // ["a","b","c"]
["a","b"].join("-"); // "a-b"
"banana".replace("a", "A"); //"bAnana" (only the first)
"banana".replaceAll("a","A"); // "bAnAnA"
"hello".includes("ll"); // true
"hello".startsWith("he"); // true
3. Indexing and length
Access a single character with str[i] or str.charAt(i). However, for characters with code points consisting of 2 units, such as emojis, [...str] decomposition is safe.
"hello".length; // 5
"abc"[1]; // "b"
4. Regular expression basics
Regular expression literals are in the part/.../flags format, and i (case-insensitive), g (global), and m (multi-line) are used.
const re = /\d+/g;
"a1b22c333".match(re); // ["1", "22", "333"]
"price 1500".replace(/\d+/, "###"); // "price ###"
Core example
| file | Contents covered |
|---|---|
01_template_literal.js |
Template literals and multilines |
02_methods.js |
trim/upper/replace/includes |
03_split_join.js |
Array ↔ String conversion |
04_regex_intro.js |
Regular Expression Matching/Substituting Basics |
⚠️ Common Mistakes
- There is a misunderstanding that the method changes the original. (immutable)
- Use
replacewithout knowing that it only changes the first part. - Past octal issue with
parseInt("08")— Always specify the decimal number asparseInt(s, 10). - You fall into a type conversion trap when comparing with
==rather than===. - The emoji length is incorrectly measured as
length.
❓ Frequently Asked Questions (FAQ)
Q1. What is the difference between " and '? A. They are the same. Follow project conventions.
Q2. How to change one character in a string? A. You must create a new string. Example: s.slice(0, i) + ch + s.slice(i+1).
Q3. Regular expression vs includes? A. includes is suitable for simple inclusion, and regular expression is suitable for pattern matching.