简体   繁体   English

如何在 javascript 中检索文本中重复字符串的第一个字符的索引?

[英]How can I retrieve in javascript the index of the first character of a string repeatedly in a text?

i must obtain in javascript the first index of the term "Query Response" that appears multiple times in "output_pacs" that is a variable in which is allocated the content, that is a file with multiple rows and columns.我必须在 javascript 中获取术语“查询响应”的第一个索引,该索引在“output_pacs”中多次出现,这是一个分配内容的变量,即具有多行和多列的文件。

var strutturaIndici=[];
for(var i=0;i< output.length;i++)
{
    var indice_primo_carattere= output(i).indexOf("Query Response");
    strutturaIndici.push(indice_primo_carattere);
}

logger.debug('StrutturaIndici:' +strutturaIndici);

The problem is that the term "Query Response" appears to me multiple times in this file, and i must obtain the first index of this term every time that it appears in this content with this code, in the console, i don't obtain the indexes, what can i do?问题是“查询响应”一词在此文件中多次出现在我面前,并且每次使用此代码出现在此内容中时,我都必须获取该词的第一个索引,在控制台中,我没有获得索引,我该怎么办? in the console i obtain this:在控制台中我得到这个:

[2021-01-30 12:53:20,002]  DEBUG  (js-connector:?): StrutturaIndici:

Just make a loop and call indexOf everytime with the lastIndex as a start to search for next term.只需创建一个循环并每次以lastIndex调用indexOf作为搜索下一个词的开始。 And if indexOf returns -1, then break out of the loop.如果indexOf返回 -1,则跳出循环。

You may look here , how the indexOf function is defined on strings你可以看看这里indexOf function 是如何在字符串上定义的

 var output_pacs = "test Query Response Query Response hello Query Response" var indices = []; var lastIndex = 0; while (true) { lastIndex = output_pacs.indexOf("Query Response", lastIndex + 1); if (lastIndex === -1) { // No more found break; } indices.push(lastIndex); } console.log('StrutturaIndici:' + indices);

Try Using:尝试使用:

 function getIndexes(searchStr, str) { let searchStrLen = searchStr.length; if (searchStrLen == 0) { return []; } let startIndex = 0, index, indices = []; str = str.toLowerCase(); searchStr = searchStr.toLowerCase(); while ((index = str.indexOf(searchStr, startIndex)) > -1) { indices.push(index); startIndex = index + searchStrLen; } return indices; } let a = getIndexes("Query Response", "Query Response Happy New Year Query Response Happy Query Response New Year Happy Pongal Query Response New Year"); console.log(a);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为字符串的第一个字符设置样式? - How can I style the first character of a string? 如何检索json中的文本索引 - How can I retrieve a index of text inside json 如何检索此字符串中定义的文件名? 文件名在我的字符串的 _ 字符之后和 * 字符之前 - How can I retrieve the file name defined inside this string? the filename is after _ character and before * character of my string 如何在InDesign中获得所选文本之后的第一个字符? - How can I get the first character after the selected text in InDesign? 如何使用 JavaScript 将句子的第一个字符大写? - How can I capitalize the first character of a sentence with JavaScript? 如何检查字符串的第一个字符是否是数字作为 *ngIf 中的条件 - How can I check if the first character of a string it is a number as a condition in *ngIf Javascript:获取字符串中第一个特殊正则表达式字符的索引 - Javascript: Get index of first special regex character in a string 如何使用javascript从包含数值的字符串中获取第一个字符的索引 - How to get the Index of First Character from a string contains numeric values using javascript 如何用JavaScript中的字符串替换第一次出现的字符? - How to replace the first occurrence of a character with a string in JavaScript? 如何识别字符串中的第一个字符是否是Javascript中的数字 - How to identify if first character in a string is a number in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM