Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Commit 7b7d258

Browse files
ganes1410Kent C. Dodds
authored andcommitted
feat: add find and replace function (#75)
1 parent a441981 commit 7b7d258

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import multiply from './multiply'
2121
import square from './square'
2222
import sum from './sum'
2323
import dec2bin from './dec2bin'
24+
import searchAndReplace from './search-and-replace'
2425

2526
export {
2627
flatten,
@@ -46,4 +47,5 @@ export {
4647
square,
4748
sum,
4849
dec2bin,
50+
searchAndReplace,
4951
}

src/search-and-replace.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export default searchAndReplace
2+
3+
/**
4+
*Original Source:
5+
*https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript/1144788#1144788
6+
*
7+
*This method will replace the matched word in the given string
8+
*string
9+
10+
* @param {String} str - The given string
11+
* @param {String} find - The particular string that we want to replace
12+
@param {String} replace - The string to replace the matched string
13+
@return {String} - The final string with replaced words
14+
*/
15+
16+
function searchAndReplace(str, find, replace) {
17+
return str.replace(new RegExp(find, 'g'), replace)
18+
}

test/search-and-replace.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import test from 'ava'
2+
import {searchAndReplace} from '../src'
3+
4+
test('check the resultant string', t => {
5+
const str = 'the quick brown fox jumps over the lazy dog';
6+
const find = 'lazy';
7+
const replace = 'active';
8+
const expected = 'the quick brown fox jumps over the active dog';
9+
const actual = searchAndReplace(str, find, replace);
10+
t.deepEqual(actual, expected);
11+
})

0 commit comments

Comments
 (0)