Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中搜索文本并获取其所有开始和结束索引_Javascript - Fatal编程技术网

在javascript中搜索文本并获取其所有开始和结束索引

在javascript中搜索文本并获取其所有开始和结束索引,javascript,Javascript,我有一个类似的内容 苹果是苹果树的果实,苹果属 蔷薇科(蔷薇科)的家蝇。它是最广泛的应用之一 栽培乔木果实中,苹果是优良和应用最广泛的 已知为人类使用的苹果属的许多成员。 苹果生长在每年落叶的小树上 我有一个类似的数组 ["apple", " ", "is", " ", "the"]; 使用此数组,如何在javascript中查找单词apple的开始索引和结束索引 我尝试循环内容并使用indexOf索引,但无法获得该单词的所有索引 这就是我试过的 var matchs =[]; var con

我有一个类似的内容

苹果是苹果树的果实,苹果属 蔷薇科(蔷薇科)的家蝇。它是最广泛的应用之一 栽培乔木果实中,苹果是优良和应用最广泛的 已知为人类使用的苹果属的许多成员。 苹果生长在每年落叶的小树上

我有一个类似的数组

["apple", " ", "is", " ", "the"];
使用此数组,如何在javascript中查找单词apple的开始索引和结束索引

我尝试循环内容并使用
indexOf
索引,但无法获得该单词的所有索引

这就是我试过的

var matchs =[];
var content = "a b c defgh a b csdwda abcdfd";
var arrayWord =["a"," ", "b"];
var w =0;
var pos =0;
var firstIndex = content.indexOf(arrayWord[w],pos);
pos = firstIndex;
while (pos > -1) {
    pos+=arrayWord[w].length;
    w++; 
    pos = content.indexOf(arrayWord[w], pos);
    matchs.push(firstIndex,pos);
}

假设我正确理解了您的问题,您可以
加入
数组,并使用字符串的
indexOf
方法获取开始索引(本例假设您的字符串存储在
str
中,数组存储在
arr
):

您还可以从数组中删除空格,并将空格传递给
join
,而不是较小的数组

var text = $("h5").text(); // Get the text from your h5.
var searchText = "apple is the";
var found = []
function findAll(string) {
  var startIdx = string.search(searchText);
  var endIdx = startIdx + searchText.length;
  if(startIdx == -1) {
    return;
  }
  else {
    found.append([startIdx, endIdx]);
    findAll(string.substring(endIdx, string.length));
  }
}
findAll(text);
这将递归搜索字符串,直到找到
searchText
的所有实例


每个事件都存储为开始和结束索引的列表
[[start,end],[start,end],…]
中找到

阅读您的评论后,我想这就是您想要的。如有必要,可以添加进一步的replace语句

var text,
    pos,
    start,
    matches = [],
    charArr,
    charText,
    currentMatch;

text = $("h5").text( );

//white spaces must match length of string being replaced
text = text.replace("\r\n","    ");
charText = text.split("");

charArr = ["apple", " ", "is", " ", "the"].join("").split("");
currentMatch = 0;

// Loop through char array ignoring multiple white spaces
for( pos = 0; pos < text.length; pos += 1 ) {

    if( currentMatch === 0 ) start = pos;

    if( charText[pos] === charArr[currentMatch] ) {
        currentMatch += 1;      
    } else if( charText[pos] !== " " ) {
        currentMatch = 0;
    }

    // matched entire array so push to matches
    if( currentMatch === charArr.length ) {     
        matches.push( [ start, pos] );
        currentMatch = 0;
    }
}
var文本,
销售时点情报系统,
开始
匹配项=[],
查拉尔,
图表文本,
电流匹配;
text=$(“h5”).text();
//空格必须匹配被替换字符串的长度
text=text.replace(“\r\n”,”);
charText=text.split(“”);
charArr=[“apple”,“is”,“the”]。join(“”)。split(“”);
currentMatch=0;
//循环通过字符数组忽略多个空格
用于(pos=0;pos

Fiddle

我没有格式化你的问题,因为我不确定你所说的“内容”是什么意思。您对文本使用了
h5
标记,您是真的在页面上有文本,还是只是有一个字符串并试图在此处使用
h5
标记格式化?请阅读我只有一个字符串并用H5格式化它。我不能加入数组并搜索,实际上我的内容可能包含不需要的空格,如\n\r等。我也需要跳过它并搜索它。我使用了arrayya,但我需要所有发生的索引。请查看我的编辑。这是在查找
字符
索引,而不是字符串索引。感谢美学,如果我使用sting.search(),我将跳过该单词,就像apple是一样\n我还需要匹配具有不需要的空格的同一单词
var text,
    pos,
    start,
    matches = [],
    charArr,
    charText,
    currentMatch;

text = $("h5").text( );

//white spaces must match length of string being replaced
text = text.replace("\r\n","    ");
charText = text.split("");

charArr = ["apple", " ", "is", " ", "the"].join("").split("");
currentMatch = 0;

// Loop through char array ignoring multiple white spaces
for( pos = 0; pos < text.length; pos += 1 ) {

    if( currentMatch === 0 ) start = pos;

    if( charText[pos] === charArr[currentMatch] ) {
        currentMatch += 1;      
    } else if( charText[pos] !== " " ) {
        currentMatch = 0;
    }

    // matched entire array so push to matches
    if( currentMatch === charArr.length ) {     
        matches.push( [ start, pos] );
        currentMatch = 0;
    }
}