Skip to content

Commit a48579f

Browse files
committed
Update str_replace.js
The formula used to get the count value is incorrect. The correct count is obtained by appending the matches found when calling split minus 1 (split.length -1).
1 parent 52be837 commit a48579f

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

functions/strings/str_replace.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ 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+
// bugfixed by: Glen Arason (http://CanadianDomainRegistry.ca)
27+
// : Corrected count calculation
2628

2729
var i = 0,
2830
j = 0,
@@ -62,9 +64,9 @@ function str_replace(search, replace, subject, count) {
6264
s[i] = (temp)
6365
.split(f[j])
6466
.join(repl);
65-
if (count && s[i] !== temp) {
66-
this.window[count] += (temp.length - s[i].length) / f[j].length;
67-
}
67+
if (count) {
68+
this.window[count] += ((temp.split(f[j])).length - 1);
69+
}
6870
}
6971
}
7072
return sa ? s : s[0];

0 commit comments

Comments
 (0)