Skip to content

Instantly share code, notes, and snippets.

@suhailgupta03
Created August 17, 2023 16:45
Show Gist options
  • Save suhailgupta03/c6eaa1056d0af298b79f420a9f912a70 to your computer and use it in GitHub Desktop.
Save suhailgupta03/c6eaa1056d0af298b79f420a9f912a70 to your computer and use it in GitHub Desktop.
let string = `Hi! Recently India celebrated its Independence Day`
// 1. Find all 'e' and replace it with 'E'
// 2. Replace the 'Independence Dat' in the sentence with 'Republic Day'
// 3. Find the index of 'India' in the string
let string2 = "hello world how are you?";
// 1. Find all the spaces in the string
@suhailgupta03
Copy link
Author

suhailgupta03 commented Aug 18, 2023

Solution for string 2:

let string2 = "hello world how are you?";

// 1. Find all the spaces in the string
let spaces = string2.match(/ /g);

console.log(spaces); // Output: [ ' ', ' ', ' ', ' ' ]

/**
* In this example, we're using the match() function with a regular expression / /g to
* find all instances of ' ' (space). The g flag makes it global, so it finds all spaces, not just the first one. 
* It returns an array containing all the matched elements.
*/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment