Methods of RegExp and String in JavaScript

Methods of RegExp and String in JavaScript

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: true if there is a match, false otherwise.
let regex = /foo/; console.log(regex.test("foobar")); // true console.log(regex.test("barfoo")); // true console.log(regex.test("bar")); // false

2. exec()

  • Purpose: Executes a search for a match in a string. Returns an array of results if found, or null if no match.
  • Syntax: regex.exec(string)
  • Returns: An array of matched results or null.
let regex = /foo/; let result = regex.exec("foobar"); console.log(result); // ["foo"] console.log(result.index); // 0 (the position where the match starts)

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 RegExp object.
let regex = /foo/; regex.compile("bar"); console.log(regex.test("bar")); // true

(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 null if no match is found.
let str = "Hello, world!"; let result = str.match(/o/); console.log(result); // ["o"]

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.
let str = "Hello, world!"; let newStr = str.replace(/world/, "JavaScript"); console.log(newStr); // "Hello, JavaScript!"

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 -1 if no match is found.
let str = "Hello, world!"; let index = str.search(/world/); console.log(index); // 7

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.
let str = "apple,banana,orange"; let fruits = str.split(/,/); console.log(fruits); // ["apple", "banana", "orange"]

5. charAt()

  • Purpose: Returns the character at a specified position in a string.
  • Syntax: string.charAt(index)
  • Returns: The character at the specified index.
let str = "Hello"; console.log(str.charAt(1)); // "e"

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.
let str = "Hello"; console.log(str.charCodeAt(1)); // 101 (Unicode value for "e")

7. concat()

  • Purpose: Concatenates two or more strings.
  • Syntax: string.concat(string1, string2, ...)
  • Returns: A new string with the concatenated values.
let str1 = "Hello"; let str2 = " world!"; console.log(str1.concat(str2)); // "Hello world!"

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.
let str = "Hello, world!"; console.log(str.slice(0, 5)); // "Hello"

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.
let str = "Hello, world!"; console.log(str.substring(7, 12)); // "world"

10. toLowerCase()

  • Purpose: Converts a string to lowercase.
  • Syntax: string.toLowerCase()
  • Returns: A new string with all characters converted to lowercase.
let str = "Hello World!"; console.log(str.toLowerCase()); // "hello world!"

11. toUpperCase()

  • Purpose: Converts a string to uppercase.
  • Syntax: string.toUpperCase()
  • Returns: A new string with all characters converted to uppercase.
let str = "Hello World!"; console.log(str.toUpperCase()); // "HELLO WORLD!"

12. trim()

  • Purpose: Removes whitespace from both ends of a string.
  • Syntax: string.trim()
  • Returns: A new string with the whitespace removed.
let str = " Hello World! "; console.log(str.trim()); // "Hello World!"

13. includes()

  • Purpose: Checks if a string contains a certain substring.
  • Syntax: string.includes(searchString)
  • Returns: true if the substring is found, false otherwise.
let str = "Hello, world!"; console.log(str.includes("world")); // true console.log(str.includes("foo")); // false

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 -1 if not found.
let str = "Hello, world!"; console.log(str.indexOf("world")); // 7

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.
let str = "Hello "; console.log(str.repeat(3)); // "Hello Hello Hello "

16. endsWith()

  • Purpose: Checks if a string ends with a certain substring.
  • Syntax: string.endsWith(searchString)
  • Returns: true if the string ends with the substring, false otherwise.
let str = "Hello, world!"; console.log(str.endsWith("world!")); // true

17. startsWith()

  • Purpose: Checks if a string starts with a certain substring.
  • Syntax: string.startsWith(searchString)
  • Returns: true if the string starts with the substring, false otherwise.
let str = "Hello, world!"; console.log(str.startsWith("Hello")); // true

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.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close