In this chapter, we will explore the replace()
method in TypeScript. This method is a built-in function that helps in replacing occurrences of a substring or pattern within a string with a new substring or pattern. Understanding how to use replace()
is useful for modifying and transforming strings based on specific criteria.
Table of Contents
- Definition
- Syntax
- Examples
- Conclusion
1. Definition
The replace()
method returns a new string with some or all matches of a pattern replaced by a replacement. The pattern can be a string or a regular expression, and the replacement can be a string or a function to be called for each match.
2. Syntax
string.replace(searchValue, replaceValue);
Parameters
searchValue
: A string or regular expression representing the substring or pattern to search for.replaceValue
: A string or function to replace the matches ofsearchValue
within the string.
Return Value
The method returns a new string with some or all matches of searchValue
replaced by replaceValue
.
3. Examples
Let's look at some examples to understand how replace()
works in TypeScript.
Example 1: Basic String Replacement
In this example, we use replace()
to replace a specific substring with a new substring.
let str: string = "Hello, TypeScript!";
let result = str.replace("TypeScript", "JavaScript");
console.log(result); // Output: Hello, JavaScript!
Example 2: Using Regular Expression
In this example, we use replace()
with a regular expression to replace all occurrences of a pattern in a string.
let str: string = "TypeScript is great. TypeScript is fun.";
let result = str.replace(/TypeScript/g, "JavaScript");
console.log(result); // Output: JavaScript is great. JavaScript is fun.
Example 3: Using a Replacement Function
In this example, we use replace()
with a function to modify the matches before replacing them.
let str: string = "The price is 100 USD.";
let result = str.replace(/\d+/g, (match) => (parseInt(match) * 1.1).toString());
console.log(result); // Output: The price is 110 USD.
Example 4: Case-Insensitive Replacement
In this example, we use replace()
with a case-insensitive regular expression to replace a substring regardless of its case.
let str: string = "Hello, TypeScript! Hello, typescript!";
let result = str.replace(/typescript/gi, "JavaScript");
console.log(result); // Output: Hello, JavaScript! Hello, JavaScript!
Example 5: Replacing Special Characters
In this example, we use replace()
to replace special characters within a string.
let str: string = "Hello, TypeScript! How's it going?";
let result = str.replace(/[^\w\s]/g, "");
console.log(result); // Output: Hello TypeScript Hows it going
4. Conclusion
In this chapter, we explored the replace()
method in TypeScript, which is used to replace occurrences of a substring or pattern within a string with a new substring or pattern. We covered its definition, syntax, parameters, return value, and provided several examples to demonstrate its usage. Understanding how to use replace()
effectively can help in various string manipulation tasks in TypeScript, especially when modifying and transforming strings based on specific criteria.
Comments
Post a Comment
Leave Comment