Skip to content

Latest commit

 

History

History
39 lines (27 loc) · 1.93 KB

40-RegExp.md

File metadata and controls

39 lines (27 loc) · 1.93 KB

Regular Expression

A regular expression, also known as regex or regexp, is a pattern that is used to match and manipulate text. It is a powerful tool for string manipulation, searching, and validation.

In JavaScript, you can create a regular expression using the RegExp object, or by using a regular expression literal. Here are some examples:

// Using the RegExp constructor
const regex1 = new RegExp("hello");
console.log(regex1.test("hello world")); // output: true

// Using a regular expression literal
const regex2 = /world/;
console.log(regex2.test("hello world")); // output: true

In the above examples, the regular expressions are used to test if a string contains a specific substring. The test() method returns true if the substring is found, and false otherwise.

Regular expressions can also contain special characters and syntax to match more complex patterns. Here are some examples:

// Matching a sequence of digits
const regex3 = /\d+/;
console.log(regex3.test("12345")); // output: true

// Matching a valid email address
const regex4 = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
console.log(regex4.test("john@example.com")); // output: true

// Replacing all occurrences of a substring
const regex5 = /apple/g;
const text = "I like apples, but not pineapples.";
console.log(text.replace(regex5, "orange")); // output: I like oranges, but not pineoranges.

In the first example, the \d+ pattern matches one or more digits in a row. In the second example, the regular expression matches a valid email address according to a specific pattern. In the third example, the replace() method is used to replace all occurrences of a substring in a string.

Regular expressions are a powerful tool for string manipulation and validation in JavaScript. They can be used for a wide range of tasks, from simple substring matching to complex pattern matching and replacement.