DEV Community

Cover image for JavaScript Regex guide for for beginners.
Vishal Nayak
Vishal Nayak

Posted on • Updated on

JavaScript Regex guide for for beginners.

JavaScript Regular Expressions, commonly known as Regex, are a powerful tool for searching and manipulating strings of text. Regex can be used to validate user input, extract information from strings, and even replace text within strings. In this article, we will explore the basics of JavaScript Regex and learn how to use them effectively.

What is a Regular Expression?

A Regular Expression is a pattern that is used to match and manipulate text. It is a sequence of characters that define a search pattern. Regular expressions are used in many programming languages, including JavaScript, to search, replace, and validate strings. They are incredibly versatile and can be used to perform a wide range of tasks, from simple string manipulation to complex text processing.

Creating a Regular Expression in JavaScript

In JavaScript, regular expressions are created using the RegExp object or by using the regular expression literal notation. Here's an example of creating a regular expression using the RegExp object:

let myRegex = new RegExp('hello');
Enter fullscreen mode Exit fullscreen mode

Here's an example of creating a regular expression using the regular expression literal notation:

let myRegex = /hello/;
Enter fullscreen mode Exit fullscreen mode

Both of these examples create a regular expression that matches the string "hello".

Matching a Regular Expression

Once you have created a regular expression, you can use it to match text using the test() method or the match() method. Here's an example of using the test() method:

let myRegex = /hello/;
let myString = 'hello world';

if (myRegex.test(myString)) {
  console.log('Match found!');
} else {
  console.log('Match not found.');
}

Enter fullscreen mode Exit fullscreen mode

This code will output "Match found!" because the regular expression matches the string "hello" within the variable myString.

The match() method is used to extract the matched text from a string. Here's an example:

let myRegex = /hello/;
let myString = 'hello world';

let matchResult = myString.match(myRegex);

console.log(matchResult);

Enter fullscreen mode Exit fullscreen mode

This code will output ["hello"], which is an array containing the matched text.

Regular Expression Modifiers

Modifiers are used to modify the behavior of regular expressions. Here are some of the most commonly used modifiers in JavaScript:

  • g: Global search - searches for all occurrences of the pattern.

  • i: Case-insensitive search - ignores case when searching for the pattern.

  • m: Multi-line search - searches for the pattern in multi-line strings.

Here's an example of using the g modifier to search for all occurrences of a pattern:

let myRegex = /hello/g;
let myString = 'hello world, hello javascript, hello regex';

let matchResult = myString.match(myRegex);

console.log(matchResult);

Enter fullscreen mode Exit fullscreen mode

This code will output ["hello", "hello", "hello"], which is an array containing all occurrences of the string "hello" within the variable myString.

Regular Expression Patterns

Regular expression patterns are used to define the search pattern. Here are some of the most commonly used patterns in JavaScript:

  • .: Matches any character except newline.
  • \d: Matches any digit character.
  • \w: Matches any word character (alphanumeric).
  • \s: Matches any whitespace character.
  • ^: Matches the beginning of a string.
  • $: Matches the end of a string.

Here's an example of using the \d pattern to match any digit character:

let myRegex = /\d/;
let myString = 'abc123def';

let matchResult = myString.match(myRegex);

console.log(matchResult);

Enter fullscreen mode Exit fullscreen mode

This code will output ["1"], which is the first digit character within the variable myString.

Conclusion

Regular expressions

Top comments (0)