../courses.php
RegEx
REGEX104
3
Allow Only Certain Characters
Character sets let one position match only selected characters.
What does a character set do?
A character set matches one character from a controlled list or range.
A character set uses square brackets.
It does not match every character inside the brackets at once.
It matches one character from the allowed choices.
Sets are safer than dots when only certain characters should be allowed.
They are common in validation, cleanup, and code searches.
Matching controlled choices
console.log(/gr[ae]y/.test("gray")); // true
console.log(/gr[ae]y/.test("grey")); // true
console.log(/gr[ae]y/.test("groy")); // false
console.log(/[0-9]/.test("Room 7")); // true
console.log(/[A-Z]/.test("abc")); // false
[ae] means a or e.
gr[ae]y matches gray or grey.
[0-9] matches one digit.
[A-Z] matches one uppercase English letter.
A set controls one character position.
Control
Sets limit what can match.
Validation
They help check allowed characters.
Variants
They handle small spelling differences.
Safety
They avoid careless wildcard matching.
Too loose
Controlled choices
Ask ChatGPT: "Should this RegEx use a character set instead of a dot?"
Write a pattern that matches gray and grey.
Write a pattern that matches one digit.
Write a pattern that matches one uppercase letter.
Explain why a set is safer than a dot.
Test one value that should fail.
Thinking a set matches the whole word inside brackets.
Using a dot when choices are known.
Forgetting that a set matches one character.
Making ranges too wide.
Not testing rejected characters.
Use character sets to match one controlled character.
Login - Jit4All
Login
Welcome back to Jit4All