Skip to content

Commit a993e51

Browse files
committed
Search and Replace
1 parent d721952 commit a993e51

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ Once inside, run the command below:
1414
5. Palindromes
1515
6. Hamming Distance
1616
7. Longest Word In a Sentence
17+
8. Search and Replace
1718

src/searchReplace/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// pick a solution and insert here to run the test.
2+
3+
4+
function searchReplace(str, word, newWord){
5+
if ( word[0] === word[0].toUpperCase() ) {
6+
newWord = newWord[0].toUpperCase() + newWord.slice(1)
7+
}
8+
return str.replace(word, newWord)
9+
}
10+
11+
module.exports = searchReplace;

src/searchReplace/solutions.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// USING .replace()
2+
3+
function searchReplace(str, word, newWord){
4+
if ( word[0] === word[0].toUpperCase() ) {
5+
newWord = newWord[0].toUpperCase() + newWord.slice(1)
6+
}
7+
return str.replace(word, newWord)
8+
}
9+
10+
11+
// USING REGULAR EXPRESSION
12+
13+
function searchReplace(str, word, newWord) {
14+
let regex = new RegExp(word, "gi");
15+
if (/[A-Z]/.test(word[0])) {
16+
newWord = newWord.charAt(0).toUpperCase() + newWord.slice(1);
17+
}
18+
return str.replace(regex, newWord)
19+
}

src/searchReplace/test.js

Whitespace-only changes.

0 commit comments

Comments
 (0)