What does RegExp mean and how to use it in JS?

Hi all. I’ve learned how to use JavaScript. Like this:

let str = "Hello world!";

However, I have a question.

I don’t know how to use RegExp in JavaScript, like this:

const str = "H3llo w0rld!";
var re = /\d/;

Can you please explain what does RegExp mean in JavaScript? And how to use it??

Pummarin :slight_smile:

Hey @pummarinbest!

A Regular expression (or regex/regexp for short) is a string of text that allows you to create patterns that help match, locate, and manage text. Regex is not only found in javascript but other programming languages like Python, C++ and Java.

One example of using regular expressions is to validate the email and password inputs for a form.

There is a great FCC article on how to validate a form using javascript.

Here is an example from the article on how to validate the email input.

  const isValidEmail = /^\S+@\S+$/g
  if (!isValidEmail.test(email)) {
    return 'Please enter a valid email';

Regular expressions can be used in other contexts as well.

For your example below you can use many methods like test, match, exec, etc. All of this will be covered in the FCC lessons.

const str = "H3llo w0rld!";
let re = /\d/ // matches any single digit

Hope that helps!

Happy coding!

1 Like