Search and Replace
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
First argument is the sentence to perform the search and replace on.
Second argument is the word that you will be replacing (before).
Third argument is what you will be replacing the second argument with (after).
Note: Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word Book
with the word dog
, it should be replaced as Dog
myReplace("Let us go to the store", "store", "mall")
should return the string Let us go to the mall
.
Passed
myReplace("He is Sleeping on the couch", "Sleeping", "sitting")
should return the string He is Sitting on the couch
.
Passed
myReplace("I think we should look up there", "up", "Down")
should return the string I think we should look down there
.
Passed
myReplace("This has a spellngi error", "spellngi", "spelling")
should return the string This has a spelling error
.
Passed
myReplace("His name is Tom", "Tom", "john")
should return the string His name is John
.
Passed
myReplace("Let us get back to more Coding", "Coding", "algorithms")
should return the string Let us get back to more Algorithms
.
function myReplace(str, before, after) {
//也可以使用charAt(0)
after = /^[A-Z]/.test(before) ? after[0].toUpperCase() + after.substr(1) : after[0].toLowerCase() + after.substr(1)
str = str.replace(before, after)
return str;
}
console.log(
myReplace("A quick brown fox jumped over the lazy dog", "jumped", "leaped")
)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-search-and-replace/16045
slice
charAt
indexOf
Code Explanation
- In this solution, regular expression
^[A-Z]
is used to check (test) if the first character of before is uppercase. - If first letter of before is capitalized, change the first letter of after to uppercase.
- Else: If first letter of before is lowercase, change the first letter of after to lowercase
- Return the new string replacing before with after.