-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSearch-And-Replace.js
54 lines (35 loc) · 1.36 KB
/
Search-And-Replace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
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 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"
*/
function myReplace(str, before, after) {
//Explode
var stringArray = str.split(" ");
var i,sanity;
for(i = 0; i < stringArray.length; i++){
if(stringArray[i] == before){
sanity = 1;
//Uppercase or Lowercase
var characterOrigin = before.charAt(0);
var characterAfter = after.charAt(0);
//convert if neccesary
if(characterOrigin != characterOrigin.toLowerCase() || characterAfter != characterAfter.toLowerCase()){
var newCharacterAfter = after.split('');
newCharacterAfter[0] = newCharacterAfter[0].toUpperCase();
newCharacterAfter = newCharacterAfter.join('');
after = newCharacterAfter;
}
stringArray[i] = after;
}
}
if(sanity != 1){
return "This has a spelling error";
}
str = stringArray.join(" ");
console.log(str);
return str;
}
myReplace("He is Sleeping on the couch", "Sleeping", "sitting");