如何获得string中的第n个事件?
我想获得ABC
2nd
出现的起始位置。 有了这样的事情:
var string = "XYZ 123 ABC 456 ABC 789 ABC"; getPosition(string, 'ABC', 2) // --> 16
你会怎么做?
function getPosition(string, subString, index) { return string.split(subString, index).join(subString).length; }
您也可以使用stringindexOf而不创build任何数组。
第二个参数是开始寻找下一场比赛的索引。
function nthIndex(str, pat, n){ var L= str.length, i= -1; while(n-- && i++<L){ i= str.indexOf(pat, i); if (i < 0) break; } return i; } var s= "XYZ 123 ABC 456 ABC 789 ABC"; nthIndex(s,'ABC',3)
/ *返回值:(数字)24 * /
根据肯尼贝克的答案,我创build了一个原型函数,如果第n次发现没有被发现而是0,它将返回-1。
String.prototype.nthIndexOf = function(pattern, n) { var i = -1; while (n-- && i++ < this.length) { i = this.indexOf(pattern, i); if (i < 0) break; } return i; }
因为recursion总是答案。
function getPosition(input, search, nth, curr, cnt) { curr = curr || 0; cnt = cnt || 0; var index = input.indexOf(search); if (curr === nth) { if (~index) { return cnt; } else { return -1; } } else { if (~index) { return getPosition(input.slice(index + search.length), search, nth, ++curr, cnt + index + search.length); } else { return -1; } } }
这是我的解决scheme,它只是遍历string,直到findn
匹配:
String.prototype.nthIndexOf = function(searchElement, n, fromElement) { n = n || 0; fromElement = fromElement || 0; while (n > 0) { fromElement = this.indexOf(searchElement, fromElement); if (fromElement < 0) { return -1; } --n; ++fromElement; } return fromElement - 1; }; var string = "XYZ 123 ABC 456 ABC 789 ABC"; console.log(string.nthIndexOf('ABC', 2)); >> 16
此方法创build一个函数,该函数调用存储在数组中的第n个事件的索引
function nthIndexOf(search, n) { var myArray = []; for(var i = 0; i < myString.length; i++) { //loop thru string to check for occurrences if(myStr.slice(i, i + search.length) === search) { //if match found... myArray.push(i); //store index of each occurrence } } return myArray[n - 1]; //first occurrence stored in index 0 }
我在StackOverflow的另一个问题上玩弄下面的代码,并认为这可能适合在这里。 函数printList2允许使用正则expression式,并按顺序列出所有的事件。 (printList是一个早期的解决scheme,但在许多情况下失败了。)
<html> <head> <title>Checking regex</title> <script> var string1 = "123xxx5yyy1234ABCxxxabc"; var search1 = /\d+/; var search2 = /\d/; var search3 = /abc/; function printList(search) { document.writeln("<p>Searching using regex: " + search + " (printList)</p>"); var list = string1.match(search); if (list == null) { document.writeln("<p>No matches</p>"); return; } // document.writeln("<p>" + list.toString() + "</p>"); // document.writeln("<p>" + typeof(list1) + "</p>"); // document.writeln("<p>" + Array.isArray(list1) + "</p>"); // document.writeln("<p>" + list1 + "</p>"); var count = list.length; document.writeln("<ul>"); for (i = 0; i < count; i++) { document.writeln("<li>" + " " + list[i] + " length=" + list[i].length + " first position=" + string1.indexOf(list[i]) + "</li>"); } document.writeln("</ul>"); } function printList2(search) { document.writeln("<p>Searching using regex: " + search + " (printList2)</p>"); var index = 0; var partial = string1; document.writeln("<ol>"); for (j = 0; j < 100; j++) { var found = partial.match(search); if (found == null) { // document.writeln("<p>not found</p>"); break; } var size = found[0].length; var loc = partial.search(search); var actloc = loc + index; document.writeln("<li>" + found[0] + " length=" + size + " first position=" + actloc); // document.writeln(" " + partial + " " + loc); partial = partial.substring(loc + size); index = index + loc + size; document.writeln("</li>"); } document.writeln("</ol>"); } </script> </head> <body> <p>Original string is <script>document.writeln(string1);</script></p> <script> printList(/\d+/g); printList2(/\d+/); printList(/\d/g); printList2(/\d/); printList(/abc/g); printList2(/abc/); printList(/ABC/gi); printList2(/ABC/i); </script> </body> </html>
使用indexOf
和recursion :
首先检查通过的第n个位置是否大于子string的总次数。 如果通过,recursion地遍历每个索引,直到find第n个索引。
var getNthPosition = function(str, sub, n) { if (n > str.split(sub).length - 1) return -1; var recursePosition = function(n) { if (n === 0) return str.indexOf(sub); return str.indexOf(sub, recursePosition(n - 1) + 1); }; return recursePosition(n); };