🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this chapter, we will explore the match() method in TypeScript. This method is a built-in function that helps in matching a string against a regular expression. Understanding how to use match() is useful for pattern matching and extracting substrings based on specific patterns.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The match() method retrieves the result of matching a string against a regular expression. It returns an array of matched substrings or null if no matches are found.
2. Syntax
string.match(regexp);
Parameters
regexp: A regular expression object. If a non-RegExp object is passed, it is implicitly converted to a RegExp with a global search.
Return Value
The method returns an array containing the matches, or null if no match is found.
3. Examples
Let's look at some examples to understand how match() works in TypeScript.
Example 1: Basic Usage
In this example, we use match() to find all occurrences of a pattern in a string.
let str: string = "Hello, TypeScript! TypeScript is awesome!";
let result = str.match(/TypeScript/g);
console.log(result); // Output: [ 'TypeScript', 'TypeScript' ]
Example 2: Using match() without Global Flag
In this example, we use match() without the global flag to find the first occurrence of a pattern in a string.
let str: string = "Hello, TypeScript! TypeScript is awesome!";
let result = str.match(/TypeScript/);
console.log(result); // Output: [ 'TypeScript', index: 7, input: 'Hello, TypeScript! TypeScript is awesome!', groups: undefined ]
Example 3: Matching Digits in a String
In this example, we use match() to find all digit characters in a string.
let str: string = "The year is 2024.";
let result = str.match(/\d+/g);
console.log(result); // Output: [ '2024' ]
Example 4: Matching Words in a String
In this example, we use match() to find all words in a string.
let str: string = "Hello, TypeScript!";
let result = str.match(/\w+/g);
console.log(result); // Output: [ 'Hello', 'TypeScript' ]
Example 5: No Match Found
In this example, we use match() with a pattern that does not exist in the string.
let str: string = "Hello, TypeScript!";
let result = str.match(/JavaScript/);
console.log(result); // Output: null
4. Conclusion
In this chapter, we explored the match() method in TypeScript, which is used to match a string against a regular expression. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use match() effectively can help in various string manipulation tasks in TypeScript, especially when working with patterns and extracting substrings based on specific criteria.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment