This repository was archived by the owner on Feb 20, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Expand file tree Collapse file tree 3 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import multiply from './multiply'
2121import square from './square'
2222import sum from './sum'
2323import dec2bin from './dec2bin'
24+ import searchAndReplace from './search-and-replace'
2425
2526export {
2627 flatten ,
@@ -46,4 +47,5 @@ export {
4647 square ,
4748 sum ,
4849 dec2bin ,
50+ searchAndReplace ,
4951}
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments