List of JavaScript Useful String Methods and how to use them
By Harshit Kumar | 2023-11-14
10 min read โฆ javascript
In the ever-evolving world of web development, one thing remains constant: text is everywhere. From simple buttons to complex forms, from user interfaces to dynamic content, text strings are the building blocks of communication. And this is where JavaScript string methods come into the limelight.
We'll dive into the world of JavaScript text tricks. I will show you how to replace text, cut it into pieces, and even customize it. With these skills, you can create great things on websites and apps. So, if you're ready, let's start this fun and useful adventure with text in JavaScript!
Accessing Elements
01. String.length
The String.length
property in JavaScript is used to retrieve the number of characters (length) in a string. It provides a simple and efficient way to determine the size of a string.
Getting the Length of a String
const myString = 'Hello, Harshit!';
const length = myString.length;
console.log(length); // Output: 15
Above, we have a string myString
containing the text "Hello, Harshit!". We use the length
property to obtain the length of the string, which is the count of characters in the string. In this case, the length is 15 characters.
Handling Empty Strings
const emptyString = '';
const emptyStringLength = emptyString.length;
console.log(emptyStringLength); // Output: 0
Even an empty string has a length property. In this example, we have an empty string, and by accessing its length property, we get a result of 0
.
Handling Unicode Surrogate Pairs
const surrogatePair = '๐จโ๐ฉโ๐งโ๐ฆ';
const surrogatePairLength = surrogatePair.length;
console.log(surrogatePairLength); // Output: 8
Unicode surrogate pairs, like emojis, are counted as two characters by the length
property. In this example, the string contains four emoji characters and is counted as eight
characters in total.
02. String.charAt()
The String.charAt()
method in JavaScript is used to retrieve the character at a specified index
within a string. It allows us to access individual characters by their position, with the first character at index 0
.
Retrieving a Character at a Specific Index
const myString = 'Hello, world!';
const characterAtIndex3 = myString.charAt(3);
console.log(characterAtIndex3); // Output: 'l'
Above, we have a string myString
containing the text Hello, world!
. We use the charAt(3)
method to access the character at index 3
, which corresponds to the letter l
. The method returns the character at the specified index.
Handling Out-of-Range Indices
const myString = 'Hello, world!';
const characterAtOutOfRangeIndex = myString.charAt(20);
console.log(characterAtOutOfRangeIndex); // Output: ''
If we specify an index that is out of the string's range, the charAt()
method returns an empty string (''). In this example, there is no character at index 20
in the string, so an empty string is returned.
The String.charAt()
is useful when we need to perform operations or validations at the character level, such as checking for specific characters, manipulating text, or extracting substrings.
03. String.charCodeAt()
The String.charCodeAt()
method in JavaScript is used to retrieve the Unicode character code at a specified index within a string. It allows us to access the numeric representation of a character in the Unicode character set.
Retrieving the Unicode Character Code at a Specific Index
const myString = 'Hello, world!';
const charCodeAtIndex3 = myString.charCodeAt(3);
console.log(charCodeAtIndex3); // Output: 108
Above, we have a string myString containing the text Hello, world!
. We use the charCodeAt(3)
method to access the Unicode character code at index 3
, which corresponds to the character l
. The method returns the character code as an integer, with l
having a Unicode character code of 108
.
If we specify an index that is out of the string's range, the
charCodeAt()
method returnsNaN
(Not-a-Number).
04. String.codePointAt()
The String.codePointAt()
method in JavaScript is used to retrieve the Unicode code point (integer representation) of the character at a specified index within a string. This method is especially useful when dealing with characters that are part of Unicode's supplementary planes, which require more than 16 bits to represent.
Retrieving the Unicode Code Point at a Specific Index
const myString = 'Hello, ๐!';
const codePointAtIndex6 = myString.codePointAt(6);
console.log(codePointAtIndex6); // Output: 127758
Above, we have a string myString
containing the text Hello, ๐!
. We use the codePointAt(6)
method to access the Unicode code point at index 6
, which corresponds to the Earth globe emoji ๐
. The method returns the code point as an integer, with ๐
having a Unicode code point of 127758
.
Manipulating Strings
01. String.concat()
The String.concat()
method in JavaScript is used to concatenate (combine) two or more strings to create a new string. It allows us to join strings together to form a longer string.
Concatenating Two or More Strings
const string1 = 'Hello, ';
const string2 = 'world!';
const concatenatedString = string1.concat(string2);
console.log(concatenatedString);
// Output: 'Hello, world!'
Above, we have two strings, string1
and string2
. We use the concat()
method on string1
to concatenate it with string2
. The result is a new string, concatenatedString
, that combines both input strings, creating the phrase Hello, world!
.
Concatenating Multiple Strings
We can use the concat()
method to concatenate multiple strings at once:
const string1 = 'Hello, ';
const string2 = 'beautiful ';
const string3 = 'world!';
const concatenatedString = string1.concat(string2, string3);
console.log(concatenatedString);
// Output: 'Hello, beautiful world!'
Chaining Concatenation
The concat()
method can also be chained to concatenate multiple strings in sequence:
const greeting = 'Hello, ';
const description = 'welcome to the ';
const location = 'JavaScript world!';
const fullGreeting = greeting.concat(description).concat(location);
console.log(fullGreeting);
// Output: 'Hello, welcome to the JavaScript world!'
02. String.trim()
The String.trim()
method in JavaScript is used to remove whitespace characters from both the beginning and end of a string. Whitespace characters include spaces, tabs, and newline characters.
Trimming Whitespace
const originalString = ' Hello, world! ';
const trimmedString = originalString.trim();
console.log(trimmedString);
// Output: 'Hello, world!'
Above, we have the originalString
, which contains extra spaces at the beginning and end. We use the trim()
method to create a new string, trimmedString
, where all leading and trailing spaces are removed. The result is a clean string without any extra whitespace.
Handling Whitespace Characters
const whitespaceString = ' \t This is a \n test \t ';
const trimmedWhitespaceString = whitespaceString.trim();
console.log(trimmedWhitespaceString);
// Output: 'This is a \n test'
The trim()
method not only removes spaces but also tabs and newline characters. In this example, the input string whitespaceString contains a mix of different whitespace characters, and trim()
effectively cleans it up.
Chaining with Other String Methods
We can chain the trim()
method with other string operations for more complex text processing:
const dirtyText = ' Remove spaces and tabs \t ';
const cleanedText = dirtyText.trim().toUpperCase();
console.log(cleanedText); // Output: 'REMOVE SPACES AND TABS'
In this example, we first use trim()
to remove leading and trailing spaces, and then we use toUpperCase()
to convert the text to uppercase.
03. String.toLowerCase()
The String.toLowerCase()
method in JavaScript is used to convert all the characters in a string to lowercase. It doesn't modify the original string but returns a new string with all alphabetic characters converted to their lowercase equivalents.
Converting a String to Lowercase
const originalString = 'Hello, World!';
const lowercaseString = originalString.toLowerCase();
console.log(lowercaseString);
// Output: 'hello, world!'
Above, we have the originalString
, which contains a mixture of uppercase and lowercase characters. We use the toLowerCase()
method to create a new string, lowercaseString
, where all alphabetic characters are converted to their lowercase form. The result is a string with all characters in lowercase.
The
toLowerCase()
method affects only alphabetic characters, leaving non-alphabetic characters (such as digits and special symbols) unchanged.
Consistent String Comparison
One common use of toLowerCase()
is for string comparisons, where we want to make the comparison case-insensitive. By converting both compared strings to lowercase, you can ensure that the comparison is not affected by letter case:
const userInput = 'UserInput';
const storedPassword = 'userinput';
if (userInput.toLowerCase() === storedPassword.toLowerCase()) {
console.log('Access granted.');
} else {
console.log('Access denied.');
}
In this example, we compare userInput
and storedPassword
in a case-insensitive manner by first converting both to lowercase.
04. String.toUpperCase()
The String.toUpperCase()
method in JavaScript is used to convert all the characters in a string to uppercase. It also doesn't modify the original string like toUpperCase()
but returns a new string with all alphabetic characters converted to their uppercase equivalents.
Converting a String to Uppercase
const originalString = 'Hello, World!';
const uppercaseString = originalString.toUpperCase();
console.log(uppercaseString); // Output: 'HELLO, WORLD!'
Like
toLowerCase()
thetoUpperCase()
method also affects only alphabetic characters, leaving non-alphabetic characters (such as digits and special symbols) unchanged.
05. String.substring()
The String.substring()
method in JavaScript is used to extract a portion of a string between two specified indices. It returns a new string containing the characters between the start and end indices, without modifying the original string.
Extracting a Substring
const originalString = 'Hello, world!';
const substring = originalString.substring(7, 12);
console.log(substring); // Output: 'world'
Above, we have the originalString
, which contains the text Hello, world!
. We use the substring(7, 12)
method to create a new string, substring
, that extracts characters between indices 7 (inclusive) and 12 (exclusive). The result is the substring world
.
Handling Negative Indices
const originalString = 'JavaScript is awesome';
const substring = originalString.substring(-10, 7);
console.log(substring); // Output: 'Java'
substring()
allows us to use negative indices to count characters from the end of the string. In this example, we specify a negative start index, and the method still extracts the substring 'Java'.
Omitting the End Index
const originalString = 'Programming is fun';
const substring = originalString.substring(14);
console.log(substring); // Output: 'fun'
In this case, we provide only the start index (14)
, and substring()
extracts the characters from that index to the end of the string.
The String.substring()
method is a useful tool for extracting specific portions of a string, making it handy for tasks like string manipulation, text extraction, and data parsing in JavaScript.
06. String.substr()
The String.substr()
method in JavaScript is used to extract a substring from a string, starting at a specified position (index) and of a specified length. It allows us to create a new string containing characters from the original string based on the starting index and the length of the desired substring.
Extracting a Substring with Start Index and Length
const originalString = 'JavaScript is awesome';
const substring = originalString.substr(11, 7);
console.log(substring); // Output: 'awesome'
In the code snippet above, we have the originalString
, which contains the text JavaScript is awesome
. We use the substr(11, 7)
method to create a new string, substring
, that starts at index 11 and includes the next 7 characters. The result is the substring 'awesome'.
Handling Negative Start Index
const originalString = 'Programming is fun';
const substring = originalString.substr(-3, 3);
console.log(substring); // Output: 'fun'
substr()
allows us to use a negative start index, indicating the number of characters counted from the end of the string. In this example, we specify a negative start index of -3, which corresponds to the last three characters in the string.
Omitting the Length Parameter
If we omit the length parameter, substr()
will extract characters from the specified start index to the end of the string:
const originalString = 'JavaScript is versatile';
const substring = originalString.substr(11);
console.log(substring); // Output: 'versatile'
07. String.replace()
The String.replace()
method in JavaScript is used to search for a specified substring or regular expression pattern within a string and replace it with another substring or a function's return value. It allows us to perform text replacement or substitution within a string.
Replacing a Substring with Another Substring
const originalString = 'Hello, world!';
const replacedString = originalString.replace('world', 'universe');
console.log(replacedString); // Output: 'Hello, universe!'
Above, we have the originalString
, which contains the text Hello, world!
We use the replace('world', 'universe')
method to search for the substring world
and replace it with universe
. The result is a new string, replacedString
, with the replacement applied.
Replacing All Occurrences of a Substring
To replace all occurrences of a substring, you can use a regular expression with the global (g) flag:
const originalString = 'Red, green, and blue are colors. Red is my favorite color.';
const replacedString = originalString.replace(/Red/g, 'Orange');
console.log(replacedString);
// Output: 'Orange, green, and blue are colors. Orange is my favorite color.'
In this example, we use a regular expression /Red/g to find and replace all occurrences of 'Red' with 'Orange'.
Using a Function as a Replacement
We can also use a function as the replacement parameter, allowing for dynamic replacements based on matched substrings:
const originalString = 'The year is 2023, and 2023 is a significant year.';
const replacedString = originalString.replace(/\d{4}/g, (match) => {
return parseInt(match) + 1;
});
console.log(replacedString);
// Output: 'The year is 2024, and 2024 is a significant year.'
In this example, we use a regular expression to match all occurrences of four-digit numbers and then use a function to increment each matched number by 1
.
Searching and Extracting
01. String.indexOf()
The String.indexOf()
method in JavaScript is used to search for the first occurrence of a specified substring within a string. It returns the index (position) of the first occurrence of the substring or -1 if the substring is not found.
Finding the Index of a Substring
const originalString = 'Hello, world!';
const indexOfWorld = originalString.indexOf('world');
console.log(indexOfWorld); // Output: 7
In the code snippet above, we have the originalString
, which contains the text "Hello, world!" We use the indexOf('world')
method to search for the substring 'world' within the string. The result is the index 7
, indicating that 'world' starts at the 7th position within the string.
If the specified substring is not found within the string,
indexOf()
returns -1.
Case-Sensitive Search
indexOf()
is case-sensitive, meaning it distinguishes between lowercase and uppercase characters:
const originalString = 'Search for Search';
const indexOfSearch = originalString.indexOf('search');
console.log(indexOfSearch); // Output: -1
In this case, the search for 'search' does not match 'Search' because of the case difference.
02. String.lastIndexOf()
The String.lastIndexOf()
method in JavaScript is used to search for the last occurrence of a specified substring within a string. It returns the index (position) of the last occurrence of the substring or -1 if the substring is not found.
Finding the Index of the Last Occurrence
const originalString = 'Hello, world! It is a beautiful world.';
const lastIndexOfWorld = originalString.lastIndexOf('world');
console.log(lastIndexOfWorld); // Output: 29
Above, we have the originalString
, which contains the text "Hello, world! It is a beautiful world." We use the lastIndexOf('world')
method to search for the last occurrence of the substring 'world' within the string. The result is the index 29, indicating that the last 'world' starts at the 29th position within the string.
Specifying a Starting Index
const originalString = 'Searching for a substring in a string';
const lastIndexOfSubstring = originalString.lastIndexOf('substring', 20);
console.log(lastIndexOfSubstring); // Output: 10
In this example, we start the search for 'substring' from the index 20, and lastIndexOf()
returns 10, indicating the position of the last occurrence of 'substring' before index 20.
Case-Sensitive Search
lastIndexOf()
method is case-sensitive, meaning it distinguishes between lowercase and uppercase characters:
const originalString = 'Search for search';
const lastIndexOfSearch = originalString.lastIndexOf('search');
console.log(lastIndexOfSearch); // Output: 0
03. String.search()
The String.search()
method in JavaScript is used to search for a specified substring or regular expression pattern within a string and return the index (position) of the first match. If the match is found, it returns the index of the first character of the match; otherwise, it returns -1.
Searching for a Substring
const originalString = 'Hello, world!';
const searchResult = originalString.search('world');
console.log(searchResult); // Output: 7
Above, we have the originalString
, which contains the text "Hello, world!" We use the search('world')
method to search for the substring 'world' within the string. The result is the index 7, indicating that 'world' starts at the 7th position within the string.
If the specified substring is not found within the string,
search()
returns -1.
We can also use a regular expression pattern with
search()
to perform more advanced searches.
04. String.match()
The String.match()
method in JavaScript is used to search for a specified substring or regular expression pattern within a string. It returns an array containing the matched substrings or pattern, or null if no match is found.
Finding Substrings or Patterns
const originalString = 'Hello, world! This is a wonderful world.';
const matches = originalString.match('world');
console.log(matches);
// Output: ['world', 'world']
Above, we have the originalString
, which contains the text "Hello, world! This is a wonderful world." We use the match('world')
method to search for the substring 'world' within the string. The result is an array with two matches, indicating that 'world' occurs twice in the string.
If the specified substring is not found within the string,
match()
returnsnull
Using a Regular Expression
We can also use a regular expression pattern with match()
to perform more advanced searches:
const originalString = 'JavaScript is fun and JavaScript is powerful.';
const matches = originalString.match(/JavaScript/g);
console.log(matches);
// Output: ['JavaScript', 'JavaScript']
In this example, we use the regular expression /JavaScript/g
to search for all occurrences of 'JavaScript'. The result is an array containing the matched substrings.
Extracting Matched Groups with Capturing Parentheses
If we use capturing parentheses in the regular expression, we can extract specific matched groups:
const originalString = 'My email is harshit@harshitclub.com';
const matches = originalString.match(/([a-z.]+)@([a-z.]+)/);
console.log(matches);
// Output: ['harshit@harshitclub.com', 'harshit', 'harshitclub.com']
In this case, the regular expression captures both the username and the domain name in separate groups, which are included in the resulting array.
05. String.includes()
The String.includes()
method in JavaScript is used to determine whether a specified substring exists within a given string. It returns a boolean value (true
or false
) based on whether the substring is found within the string.
Checking for Substring Existence
const originalString = 'Hello, world!';
const includesWorld = originalString.includes('world');
console.log(includesWorld); // Output: true
Above, we have the originalString
, which contains the text "Hello, world!" We use the includes('world')
method to check whether the substring 'world' exists within the string. The result is true, indicating that 'world' is found in the string.
If the specified substring is not found within the string, includes()
returns false
Case Sensitivity
const originalString = 'JavaScript is fun and javascript is powerful.';
const includesJavaScript = originalString.includes('JavaScript');
console.log(includesJavaScript); // Output: true
In this case, 'JavaScript' is considered different from 'javascript,' and includes()
returns true when searching for the exact case.
Specifying a Starting Index
We can specify a starting index for the search using the second parameter of includes()
. This restricts the search to characters beyond the specified index:
const originalString = 'Searching for a substring in a string';
const includesSubstring = originalString.includes('substring', 20);
console.log(includesSubstring); // Output: true
We start the search for 'substring' from the index 20, and includes()
returns true
, indicating that 'substring' exists in the string beyond index 20
.
06. String.startsWith()
The String.startsWith()
method in JavaScript is used to determine whether a string starts with a specified substring. It returns a boolean value (true
or false
) based on whether the string begins with the specified substring.
Checking for Substring at the Beginning
const originalString = 'Hello, world!';
const startsWithHello = originalString.startsWith('Hello');
console.log(startsWithHello); // Output: true
Above, we have the originalString
, which contains the text "Hello, world!" We use the startsWith('Hello')
method to check whether the string starts with the substring 'Hello'. The result is true
, indicating that the string begins with 'Hello'.
If the specified substring is not found at the beginning of the string, startsWith()
returns false
.
startsWith()
is case-sensitive, meaning it distinguishes between lowercase
and uppercase characters
.
07. String.endsWith()
The String.endsWith()
method in JavaScript is used to determine whether a string ends with a specified substring. It returns a boolean value (true
or false
) based on whether the string ends with the specified substring.
Checking for Substring at the End
const originalString = 'Hello, world!';
const endsWithWorld = originalString.endsWith('world!');
console.log(endsWithWorld); // Output: true
Above, we have the originalString
, which contains the text "Hello, world!" We use the endsWith('world!')
method to check whether the string ends with the substring 'world!'. The result is true
, indicating that the string ends with 'world!'.
If the specified substring is not found at the end of the string, endsWith()
returns false
.
endsWith()
is case-sensitive, meaning it distinguishes between lowercase and uppercase characters.
08. String.slice()
The String.slice()
method in JavaScript is used to extract a portion of a string and return it as a new string. This method takes one or two parameters: the starting index
(inclusive) and the ending index
(exclusive).
Extracting a Substring
const originalString = 'Hello, world!';
const slicedSubstring = originalString.slice(7, 12);
console.log(slicedSubstring); // Output: 'world'
Above, we have the originalString
, which contains the text "Hello, world!" We use the slice(7, 12)
method to extract a substring from the 7th character (inclusive) to the 12th character (exclusive). The result is a new string containing 'world'.
Handling Negative Indices
We can use negative indices to count from the end of the string:
const originalString = 'This is a sample string';
const slicedSubstring = originalString.slice(-6, -1);
console.log(slicedSubstring); // Output: 'strin'
In this example, slice(-6, -1)
extracts a substring starting from the 6th character from the end (inclusive) and ending at the 1st character from the end (exclusive).
Omitting the Ending Index
If We omit the ending index, slice()
will extract from the starting index to the end of the string:
const originalString = 'Extract this part';
const slicedSubstring = originalString.slice(8);
console.log(slicedSubstring); // Output: 'this part'
Here, we only specify the starting index, and slice(8)
extracts the substring from the 8th character to the end of the string.
If the starting index is beyond the length of the string, slice()
returns an empty string.
09. String.split()
The String.split()
method in JavaScript is used to split a string into an array of substrings based on a specified delimiter or regular expression pattern. It allows us to break a single string into smaller parts, which are then stored in an array.
Splitting a String Using a Delimiter
const originalString = 'apple,banana,cherry';
const splitArray = originalString.split(',');
console.log(splitArray);
// Output: ['apple', 'banana', 'cherry']
Above, we have the originalString
, which contains the text "apple,banana,cherry." We use the split(',')
method to split the string into an array using a comma ,
as the delimiter. The result is an array containing the substrings 'apple', 'banana', and 'cherry'.
Encoding and Decoding
01. String.fromCharCode
The String.fromCharCode()
method in JavaScript is used to create a new string from a sequence of Unicode character code points. It takes one or more numeric values representing Unicode character codes and returns the corresponding string.
Creating a String from Character Codes
const charCode1 = 65; // The Unicode code for 'A'
const charCode2 = 66; // The Unicode code for 'B'
const result = String.fromCharCode(charCode1, charCode2);
console.log(result); // Output: 'AB'
Above, we have two numeric values, charCode1
and charCode2
, representing the Unicode character codes for 'A' and 'B' respectively. We use String.fromCharCode(charCode1, charCode2)
to create a new string by converting these character codes into their corresponding characters. The result is the string 'AB'.
Creating a String from an Array of Character Codes
const charCodes = [72, 101, 108, 108, 111]; // Unicode codes for 'Hello'
const result = String.fromCharCode(...charCodes);
console.log(result); // Output: 'Hello'
In this example, we use the spread operator (...
) to pass an array of character codes to String.fromCharCode()
, which combines them into a string.
Locale-specific Formatting
01. String.localeCompare()
The String.localeCompare()
method in JavaScript is used to compare two strings based on their locale, taking into account language and cultural differences. It returns a numeric value that indicates the relationship between the two strings, allowing you to determine their order for sorting purposes.
Comparing Two Strings
const string1 = 'apple';
const string2 = 'banana';
const result = string1.localeCompare(string2);
console.log(result);
Above, we have two strings, string1
and string2
, representing the words 'apple' and 'banana'. We use the localeCompare()
method on string1
with string2
as the argument. The localeCompare()
method returns a numeric value:
- If
result
is a negative number (e.g., -1), it means thatstring1
comes beforestring2
in the locale-specific order. - If
result
is zero, it means that the two strings are equal in terms of the locale. - If
result
is a positive number (e.g., 1), it means thatstring1
comes afterstring2
in the locale-specific order.
Padding
01. String.padStart()
The String.padStart()
method in JavaScript is used to pad the beginning of a string with a specified character or characters until the resulting string reaches a desired length. It's commonly used for formatting strings, aligning text, and ensuring consistent string lengths.
Padding a String to a Fixed Length
const originalString = '42';
const paddedString = originalString.padStart(5, '0');
console.log(paddedString); // Output: '00042'
Above, we have the originalString
, which contains the text '42'. We use the padStart(5, '0')
method on originalString
to pad the beginning of the string with '0' characters until it reaches a length of 5 characters. The result is the string '00042'.
Specifying the Total Length
We can specify the desired total length for the padded string using the first parameter of padStart()
. If the original string is already longer than the specified length, no padding is added.
const originalString = 'JavaScript';
const paddedString = originalString.padStart(12, '-');
console.log(paddedString); // Output: 'JavaScript'
Padding with Different Characters
We can use characters other than '0' for padding. For example, We can use spaces, hyphens, or any other character.
const originalString = 'Hello';
const paddedString = originalString.padStart(10, '*');
console.log(paddedString); // Output: '*****Hello'
02. String.padEnd()
The String.padEnd()
method in JavaScript is used to pad the end of a string with a specified character or characters until the resulting string reaches a desired length. It's commonly used for formatting strings, aligning text, and ensuring consistent string lengths.
Padding a String to a Fixed Length
const originalString = '42';
const paddedString = originalString.padEnd(5, '0');
console.log(paddedString); // Output: '42000'
Iterating
01. String.matchAll()
The String.matchAll()
method in JavaScript helps us find and collect specific words or patterns within a sentence. Imagine we have a list of names in a sentence, and you want to find each name and its age.
Finding Names and Ages
const sentence = 'John: 25, Mary: 30, Sam: 22';
// We use a regular expression-like pattern to find names and ages
const pattern = /(\w+): (\d+)/g;
// We use matchAll to find all the names and ages in the sentence
const matches = sentence.matchAll(pattern);
// Let's loop through the matches and print them
for (const match of matches) {
const name = match[1]; // We get the name from the first part
const age = match[2]; // We get the age from the second part
console.log(`${name} is ${age} years old.`);
}
We have a sentence containing names
and ages
separated by colons and commas. We want to find and print each name and its corresponding age. We use a "pattern" to describe the name
and age
format, and then we use String.matchAll()
to find all the matches in the sentence.
By looping through the "matches" we found, we can access and print the names and ages. This method is helpful when you need to find and collect specific information from a longer piece of text, like names and ages in a list.
Conclusion
In conclusion, JavaScript offers a robust set of string methods that empower developers to manipulate, analyze, and format text with precision and elegance. Whether you're a seasoned developer or just starting your coding journey, these string methods are indispensable tools in your toolkit.
Just Keep Practicing.