Methods of RegExp and String in JavaScript
In JavaScript, both RegExp (regular expression) and String objects have several methods that allow you to work with patterns and text efficiently. Below is an overview of common methods available for both objects.
Methods of RegExp Object
A RegExp object is used for pattern matching and text manipulation in JavaScript. Here are some of the key methods of the RegExp object:
1. test()
- Purpose: Tests if a pattern matches a string.
- Syntax:
regex.test(string) - Returns:
trueif there is a match,falseotherwise.
2. exec()
- Purpose: Executes a search for a match in a string. Returns an array of results if found, or
nullif no match. - Syntax:
regex.exec(string) - Returns: An array of matched results or
null.
3. compile() (deprecated)
- Purpose: Recompiles a regular expression object with a new pattern or flags.
- Syntax:
regex.compile(pattern, flags) - Returns: A reference to the modified
RegExpobject.
(Note: This method is deprecated and generally not recommended.)
Methods of String Object
The String object in JavaScript provides many methods that allow manipulation of strings, searching patterns, and extracting substrings.
1. match()
- Purpose: Matches a string against a regular expression.
- Syntax:
string.match(regex) - Returns: An array of matches or
nullif no match is found.
2. replace()
- Purpose: Replaces part of a string that matches a regular expression with a specified replacement.
- Syntax:
string.replace(regex, replacement) - Returns: A new string with the replacements.
3. search()
- Purpose: Executes a search for a match in the string using a regular expression.
- Syntax:
string.search(regex) - Returns: The index of the first match, or
-1if no match is found.
4. split()
- Purpose: Splits a string into an array of substrings based on a regular expression or delimiter.
- Syntax:
string.split(regex) - Returns: An array of substrings.
5. charAt()
- Purpose: Returns the character at a specified position in a string.
- Syntax:
string.charAt(index) - Returns: The character at the specified index.
6. charCodeAt()
- Purpose: Returns the Unicode value of the character at a specified position in the string.
- Syntax:
string.charCodeAt(index) - Returns: The Unicode value of the character at the specified index.
7. concat()
- Purpose: Concatenates two or more strings.
- Syntax:
string.concat(string1, string2, ...) - Returns: A new string with the concatenated values.
8. slice()
- Purpose: Extracts a section of a string and returns it as a new string.
- Syntax:
string.slice(startIndex, endIndex) - Returns: A new string containing the extracted section.
9. substring()
- Purpose: Returns a substring between two indices, similar to
slice(), but with different handling of negative indices. - Syntax:
string.substring(startIndex, endIndex) - Returns: A new string containing the extracted section.
10. toLowerCase()
- Purpose: Converts a string to lowercase.
- Syntax:
string.toLowerCase() - Returns: A new string with all characters converted to lowercase.
11. toUpperCase()
- Purpose: Converts a string to uppercase.
- Syntax:
string.toUpperCase() - Returns: A new string with all characters converted to uppercase.
12. trim()
- Purpose: Removes whitespace from both ends of a string.
- Syntax:
string.trim() - Returns: A new string with the whitespace removed.
13. includes()
- Purpose: Checks if a string contains a certain substring.
- Syntax:
string.includes(searchString) - Returns:
trueif the substring is found,falseotherwise.
14. indexOf()
- Purpose: Returns the index of the first occurrence of a substring in the string.
- Syntax:
string.indexOf(searchString) - Returns: The index of the first occurrence or
-1if not found.
15. repeat()
- Purpose: Returns a new string that repeats the original string a specified number of times.
- Syntax:
string.repeat(count) - Returns: A new string with the specified number of repetitions.
16. endsWith()
- Purpose: Checks if a string ends with a certain substring.
- Syntax:
string.endsWith(searchString) - Returns:
trueif the string ends with the substring,falseotherwise.
17. startsWith()
- Purpose: Checks if a string starts with a certain substring.
- Syntax:
string.startsWith(searchString) - Returns:
trueif the string starts with the substring,falseotherwise.
Summary of Key Methods
- RegExp Methods:
test(),exec(),compile()(deprecated). - String Methods:
match(),replace(),search(),split(),charAt(),charCodeAt(),concat(),slice(),substring(),toLowerCase(),toUpperCase(),trim(),includes(),indexOf(),repeat(),endsWith(),startsWith().
Both RegExp and String offer powerful and flexible methods for string manipulation and pattern matching in JavaScript.

