1
- function substr_count ( haystack , needle , offset , length ) {
2
- // http://kevin.vanzonneveld.net
3
- // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4
- // + bugfixed by: Onno Marsman
5
- // * example 1: substr_count('Kevin van Zonneveld', 'e');
6
- // * returns 1: 3
7
- // * example 2: substr_count('Kevin van Zonneveld', 'K', 1);
8
- // * returns 2: 0
9
- // * example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
10
- // * returns 3: false
11
-
12
- var pos = 0 , cnt = 0 ;
13
-
14
- haystack += '' ;
15
- needle += '' ;
16
- if ( isNaN ( offset ) ) { offset = 0 ; }
17
- if ( isNaN ( length ) ) { length = 0 ; }
18
- offset -- ;
19
-
20
- while ( ( offset = haystack . indexOf ( needle , offset + 1 ) ) != - 1 ) {
21
- if ( length > 0 && ( offset + needle . length ) > length ) {
22
- return false ;
23
- } else {
24
- cnt ++ ;
25
- }
26
- }
27
-
28
- return cnt ;
1
+ function substr_count ( haystack , needle , offset , length ) {
2
+ // http://kevin.vanzonneveld.net
3
+ // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4
+ // + bugfixed by: Onno Marsman
5
+ // * example 1: substr_count('Kevin van Zonneveld', 'e');
6
+ // * returns 1: 3
7
+ // * example 2: substr_count('Kevin van Zonneveld', 'K', 1);
8
+ // * returns 2: 0
9
+ // * example 3: substr_count('Kevin van Zonneveld', 'Z', 0, 10);
10
+ // * returns 3: false
11
+
12
+ var pos = 0 , cnt = 0 ;
13
+
14
+ haystack += '' ;
15
+ needle += '' ;
16
+ if ( isNaN ( offset ) ) { offset = 0 ; }
17
+ if ( isNaN ( length ) ) { length = 0 ; }
18
+ offset -- ;
19
+
20
+ while ( ( offset = haystack . indexOf ( needle , offset + 1 ) ) != - 1 ) {
21
+ if ( length > 0 && ( offset + needle . length ) > length ) {
22
+ return false ;
23
+ }
24
+ cnt ++ ;
25
+ }
26
+
27
+ return cnt ;
29
28
}
0 commit comments