Regular Expressions in JavaScript

Regular Expressions in JavaScript allow pattern matching and text manipulation using special syntax and rules.

regular-expressions-in-javascript

Regular Expressions in JavaScript

Regular expressions are a powerful tool for matching patterns in strings. They are used for a variety of tasks in programming, from validating input to parsing data. In JavaScript, regular expressions are implemented through the RegExp object and several string methods that support pattern matching.

1. test() Method

The test() method searches for a match between a regular expression and a specified string. It returns true or false.

Example:

const regex = /hello/;
const str = "hello world";
console.log(regex.test(str)); // true

2. exec() Method

The exec() method searches for a match in a specified string. It returns a result array, or null.

Example:

const regex = /hello/;
const str = "hello world";
const result = regex.exec(str);
console.log(result); // ["hello", index: 0, input: "hello world", groups: undefined]

3. match() Method

The match() method retrieves the matches when matching a string against a regular expression. It returns an array of matches, or null.

Example:

const str = "hello world";
const regex = /hello/;
const result = str.match(regex);
console.log(result); // ["hello"]

4. search() Method

The search() method executes a search for a match between a regular expression and a specified string. It returns the index of the match, or -1.

Example:

const str = "hello world";
const regex = /world/;
const result = str.search(regex);
console.log(result); // 6

5. replace() Method

The replace() method searches a string for a specified value or a regular expression, and returns a new string with the replacements.

Example:

const str = "hello world";
const result = str.replace(/world/, "JavaScript");
console.log(result); // "hello JavaScript"

6. split() Method

The split() method splits a string into an array of substrings, using a specified separator (a regular expression or a fixed string).

Example:

const str = "hello world";
const result = str.split(/ /);
console.log(result); // ["hello", "world"]

7. toString() Method

The toString() method returns a string representing the specified RegExp object.

Example:

const regex = /hello/;
console.log(regex.toString()); // "/hello/"

8. Flags

Regular expressions can have flags that modify their behavior. The flags are:

  • g - global search
  • i - case-insensitive search
  • m - multi-line search
  • u - Unicode; treat a pattern as a sequence of Unicode code points
  • y - perform a "sticky" search that matches starting at the current position in the target string

Example:

const regex = /hello/gi;

9. source Property

The source property returns a string containing the source text of the regular expression.

Example:

const regex = /hello/;
console.log(regex.source); // "hello"

10. sticky Property

The sticky property indicates whether or not the y flag is used with the regular expression.

Example:

const regex = /hello/y;
console.log(regex.sticky); // true

Conclusion

Regular expressions are a versatile tool for developers working with strings in JavaScript. Understanding and utilizing these methods and properties can greatly enhance your ability to manipulate and analyze text.

Like, Share and Subscribe #DevTools99 for more useful videos, tools info and tutorials . Thank you!

</


DevTools99

Developer Co-Team

Let us make it easier for developers throughout the world to breathe. Let's keep things simple. Let's take it easy.