Regular Expression is a special expression used to search, analyze, and replace character strings in text. This expression is formed from special characters and syntax rules. With Regular Expression, we can define patterns to search for in text in a flexible and powerful way.
To search for a pattern in a text, we use the .match() or .test() method.
The .match() method returns an array containing the results of searching for a pattern in a string.
const text = "Hello, world!";
const pattern = /world/;
const result = text.match(pattern);
console.log(result); // Output: ["world"]
The .test() method returns true if the pattern is found in the text, otherwise it returns false.
const text = "Hello, world!";
const pattern = /world/;
const isMatch = pattern.test(text);
console.log(isMatch); // Output: true
To replace a pattern in a text, we use the .replace() method. This method returns a new string with the characters replaced.
const text = "Hello, world!";
const pattern = /world/;
const newText = text.replace(pattern, "universe");
console.log(newText); // Output: "Hello, universe!"
We can also use Regular Expression to extract necessary information from the text. See the example below:
const text = "My email address is example@example.com";
const pattern = /(\w+)@(\w+)\.com/;
const result = text.match(pattern);
console.log(result[0]); // Output: "example@example.com"
console.log(result[1]); // Output: "example"
console.log(result[2]); // Output: "example"
In the example above, we use Regular Expression to search for and extract email addresses from text. The result is an array containing the matching results. We can access these results through indexes in the array.
The character . represents any character except a line break.
const text = "Hello, world!";
const pattern = /H.llo/;
const isMatch = pattern.test(text);
console.log(isMatch); // Output: true
In the example above, the pattern /H.llo/ will search for strings with "H" at the beginning, followed by any character, and then "llo". The result is that "Hello" will match this pattern.
The [] character in Regular Expression is used to group a set of accepted characters.
const text = "000.000."
const pattern = /(\d{3}\.)+/
const isMatch = pattern.test(text);
console.log(isMatch); //Output : true
In the example above, the pattern /(\d{3}\.)+/ will find patterns with 3 numbers and 1 dot character after, this pattern can repeat at least 1 time.
The [] character in Regular Expression is used to determine a set of accepted characters.
const text = "Hello, world!";
const pattern = /[Hh]ello/;
const isMatch = pattern.test(text);
console.log(isMatch); // Output: true
In the example above, the pattern /[Hh]ello/ will search for a string starting with the letter "H" or "h", followed by "ello". The result is that "Hello" will match this pattern.
You can also use the "-" sign to specify a range of characters. For example:
const text = "Hello, world!";
const pattern = /[A-Za-z]+/;
const result = text.match(pattern);
console.log(result); // Output: ["Hello", "world"]
In the example above, the pattern /[A-Za-z]+/ will search for a string with at least one alphabetical character. The result is ["Hello", "world"] will match this pattern.
The ^ character in Regular Expression is used to determine the start point of the text, while the $ character is used to determine the end point of the text. Example:
const text = "Hello, world!";
const pattern = /^Hello/;
const isMatch = pattern.test(text);
console.log(isMatch); // Output: true
In the example above, the pattern /^Hello/ will search for a string starting with "Hello". The result is that "Hello" will match this pattern.
const text = "Hello, world!";
const pattern = /world!$/;
const isMatch = pattern.test(text);
console.log(isMatch); // Output: true
In the example above, the pattern /world!$/ will search for a string with "world!" at the end. The result is that "world!" will match this pattern.
The * character in Regular Expression allows the character before it to appear 0 or more times.
The + character allows the preceding character to appear 1 or more times.
The ? character allows the preceding character to appear 0 or 1 time.
The {} character allows you to specify a range of occurrences, for example {2,5} means the character repeats at least 1 time and at most 5 times
Example:
const text = "Heeeello, world!";
const pattern = /He*llo/;
const isMatch = pattern.test(text);
console.log(isMatch); // Output: true
In the above example, the pattern /He*llo/ will search for a string that starts with "H", followed by 0 or more "e" characters, and then "llo". The result is that "Heeeello" will match this pattern.
\d: Represents any digit.
\D: Represents any character that is not a digit.
\w: Represents any alphanumeric character.
\W : Represents any character that is not a letter or number.
\s: Represents a whitespace or space character.
const text = "Hello, 123!";
const pattern = /\d+/;
const result = text.match(pattern);
console.log(result); // Output: ["123"]
In the above example, the pattern /\d+/ will search for a string that has at least one digit. The result is ["123"] will match this pattern.