Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

Regular Expressions – `RegExp` & | JavaScript Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. JavaScript Course

Lesson 18 of 55

·javascript

Regular Expressions – `RegExp` & String Methods

9 mins

Course progress33%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Regular expressions are **stateful** when the global (`g`) flag is used – remember to reset `lastIndex` if reusing.
  • Escape user‑generated data before inserting into HTML to avoid XSS.
  • The **`RegExp` constructor** (`new RegExp(pattern, flags)`) lets you build patterns dynamically.

Create a regex:

const emailRE = /^[\w.-]+@[\w.-]+\.\w{2,}$/i;

Common string methods:

  • match, replace, search, split.

Example: Validate & sanitize input

function sanitize(input){
  return input.replace(/[<>"'&]/g, c=>({
    '<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;','&':'&amp;'}[c]));
}

RegExp flags – g (global), i (ignore case), m (multiline), u (unicode), y (sticky).

Make it stickPractice what you've learned1 quiz · 7 practical tests · Optional activities from JavaScript CourseBrowse activities
Previous lessonWorking with Dates – `Date` object & IntlContinue to lesson 19Array Methods – map, filter, reduce, find, some, everyBack to JavaScript Course curriculum