- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Regex Anchors
JavaScript Regular Expressions
JavaScript Regex Anchors
Anchors tie a pattern to the start or end of the text.
Without anchors, a pattern can match anywhere inside the text.
That is the single biggest cause of validation letting bad input through.
Example
Example
javascript
console.log(/\d{4}/.test("year 2026 here"));
console.log(/^\d{4}$/.test("year 2026 here"));The output is true then false.
