Skip to content

Commit 251c1b1

Browse files
committed
Merge pull request locutusjs#156 from glenara/patch-5
Update str_replace.js
2 parents 8a58ae0 + 066c718 commit 251c1b1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

functions/strings/str_replace.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ function str_replace(search, replace, subject, count) {
2323
// bugfixed by: Glen Arason (http://CanadianDomainRegistry.ca)
2424
// example 3: str_replace(Array('S','F'),'x','ASDFASDF');
2525
// returns 3: 'AxDxAxDx'
26-
26+
// bugfixed by: Glen Arason (http://CanadianDomainRegistry.ca) Corrected count
27+
// example 4: str_replace(['A','D'], ['x','y'] , 'ASDFASDF' , 'cnt');
28+
// returns 4: 'xSyFxSyF' // cnt = 0 (incorrect before fix)
29+
// returns 4: 'xSyFxSyF' // cnt = 4 (correct after fix)
30+
2731
var i = 0,
2832
j = 0,
2933
temp = '',
@@ -62,9 +66,9 @@ function str_replace(search, replace, subject, count) {
6266
s[i] = (temp)
6367
.split(f[j])
6468
.join(repl);
65-
if (count && s[i] !== temp) {
66-
this.window[count] += (temp.length - s[i].length) / f[j].length;
67-
}
69+
if (count) {
70+
this.window[count] += ((temp.split(f[j])).length - 1);
71+
}
6872
}
6973
}
7074
return sa ? s : s[0];

0 commit comments

Comments
 (0)