在字符串中查找文本,将其存储在数组中并显示它

时间:2023-01-05 22:26:24

I am trying to find a strings in a string content and pushing the found content letter by letter in an array and display all the found content in html. But it seems like something wrong with the for loop (do not run it crashes). What would be the best practice to achieve something like this without using regex. Code is located below.

我试图在字符串内容中找到一个字符串,并在一个数组中逐个字母地推送找到的内容,并在html中显示所有找到的内容。但是for循环似乎有问题(不要运行它崩溃)。在不使用正则表达式的情况下实现这样的事情的最佳实践是什么。代码位于下方。

var text = "asdas John asd asda sdas dasd asd Jon asdas das dasdas dasdasdasda sadas John jsadjasd";
var hits = [];
var myName = "John";


for (var i = text.indexOf("John"); i < (i + myName.length); i++) {
    hits.push(text[i]);
}


if (hits.length === 0) {
    document.getElementById("result").innerHTML = "not found";
} else {
    document.getElementById("result").innerHTML = hits.toString();
}

4 个解决方案

#1


Actual Problem

A positive number when added to some other positive number, will always be smaller than the same number. So,

添加到其他正数时的正数将始终小于相同的数字。所以,

i < (i + myName.length)

will always be true, since i has a positive value 6, which is the index of first occurrence of John. So, your program runs into an infinite loop.

永远都是真的,因为我有一个正值6,这是约翰第一次出现的指数。所以,你的程序会遇到无限循环。


Solution

To fix this, you can make use of the second parameter in Array.prototype.indexOf, like this

要解决此问题,您可以使用Array.prototype.indexOf中的第二个参数,就像这样

for(var i = text.indexOf("John"); i !== -1; i = text.indexOf("John", i + 1))

Now, your program will run, till i becomes -1. i = text.indexOf("John", i + 1) finds John from the index i + 1 and assigns it back to i. So, if no more Johns found, i will be -1 and the loop will break.

现在,你的程序将运行,直到我变为-1。 i = text.indexOf(“John”,i + 1)从索引i + 1中找到John并将其分配给i。所以,如果没有找到Johns,我将为-1并且循环将会中断。

Demo

var text = "asdas John asd asda sdas dasd asd Jon asdas" +
  " das dasdas dasdasdasda sadas John jsadjasd";
var hits = [];
var myName = "John";

for (var i = text.indexOf(myName); i !== -1; i = text.indexOf(myName, i + 1)) {
  hits.push(i);
}

if (hits.length === 0) {
  document.getElementById("result").innerHTML = "not found";
} else {
  document.getElementById("result").innerHTML = hits.join("<br />");
}
<pre id="result" />


What would be the best practice to achieve something like this without using regex

在不使用正则表达式的情况下实现这样的事情的最佳实践是什么

for loop is more suitable for any countable loop. Whenever you want to loop till a certain condition is true, the best choice would be a while loop.

for循环更适合任何可数循环。每当你想循环直到某个条件为真时,最好的选择是while循环。

var text = "asdas John asd asda sdas dasd asd Jon asdas" +
  " das dasdas dasdasdasda sadas John jsadjasd";
var hits = [];
var myName = "John";

var i = text.indexOf(myName);

while (i !== -1) {
  hits.push(i);
  // Find index of `myName` from the index `i + 1` and store it back it in `i`
  i = text.indexOf(myName, i + 1);
}

if (hits.length === 0) {
  document.getElementById("result").innerHTML = "not found";
} else {
  document.getElementById("result").innerHTML = hits.join("<br />");
}
<pre id="result" />

Now, the while loop makes the logic more readable and clearer. Moreover, you can have enough comments inside the loop to explain the logic, even if it is a bit difficult to understand :-)

现在,while循环使逻辑更具可读性和清晰性。而且,你可以在循环内部有足够的注释来解释逻辑,即使它有点难以理解:-)

#2


Your mostly there, I'm not sure if I completely understand what you're trying to do, but your for loop isn't actually looking for the text more than once. Plus indexOf will return <0 if it finds nothing.

你大部分都在那里,我不确定我是否完全明白你想要做什么,但是你的for循环实际上不是在寻找文本多次。如果没有找到任何内容,indexOf将返回<0。

var text="asdas John asd asda sdas dasd asd Jon asdas das dasdas " +
         "dasdasdasda sadas John jsadjasd";
var hits = [];
var myName = "John";

var find = text.indexOf(myName);
while (find >= 0) {
   hits.push(text.substr(find, myName.length));
   find = text.indexOf(myName, find + 1);
}


if (hits.length === 0) {
  document.getElementById("result").innerHTML = "not found"; 
} else {
  document.getElementById("result").innerHTML = hits.toString(); 
}

#3


Try utilizing do while loop

尝试使用do while循环

var text = "asdas John asd asda sdas dasd asd Jon asdas das dasdas "        
           + "dasdasdasda sadas John jsadjasd";
    var hits = [];
    var myName = "John";
    var len = myName.length;
    var i = text.indexOf(myName);
    var result = document.getElementById("result");
    
    do {
      // push letters of `myName`
      hits.push(text[i]);
      // increment `i`
      ++i;
    } while(hits.length < len);
    
if (hits.length === 0) {
  result.innerHTML = "not found"; 
} else {
  result.innerHTML = hits.toString(); 
}
<div id="result"></div>

#4


The condition in for loop will always be true. i will always be smaller than i plus something...

for循环中的条件始终为true。我将永远小于我加上一些东西......

#1


Actual Problem

A positive number when added to some other positive number, will always be smaller than the same number. So,

添加到其他正数时的正数将始终小于相同的数字。所以,

i < (i + myName.length)

will always be true, since i has a positive value 6, which is the index of first occurrence of John. So, your program runs into an infinite loop.

永远都是真的,因为我有一个正值6,这是约翰第一次出现的指数。所以,你的程序会遇到无限循环。


Solution

To fix this, you can make use of the second parameter in Array.prototype.indexOf, like this

要解决此问题,您可以使用Array.prototype.indexOf中的第二个参数,就像这样

for(var i = text.indexOf("John"); i !== -1; i = text.indexOf("John", i + 1))

Now, your program will run, till i becomes -1. i = text.indexOf("John", i + 1) finds John from the index i + 1 and assigns it back to i. So, if no more Johns found, i will be -1 and the loop will break.

现在,你的程序将运行,直到我变为-1。 i = text.indexOf(“John”,i + 1)从索引i + 1中找到John并将其分配给i。所以,如果没有找到Johns,我将为-1并且循环将会中断。

Demo

var text = "asdas John asd asda sdas dasd asd Jon asdas" +
  " das dasdas dasdasdasda sadas John jsadjasd";
var hits = [];
var myName = "John";

for (var i = text.indexOf(myName); i !== -1; i = text.indexOf(myName, i + 1)) {
  hits.push(i);
}

if (hits.length === 0) {
  document.getElementById("result").innerHTML = "not found";
} else {
  document.getElementById("result").innerHTML = hits.join("<br />");
}
<pre id="result" />


What would be the best practice to achieve something like this without using regex

在不使用正则表达式的情况下实现这样的事情的最佳实践是什么

for loop is more suitable for any countable loop. Whenever you want to loop till a certain condition is true, the best choice would be a while loop.

for循环更适合任何可数循环。每当你想循环直到某个条件为真时,最好的选择是while循环。

var text = "asdas John asd asda sdas dasd asd Jon asdas" +
  " das dasdas dasdasdasda sadas John jsadjasd";
var hits = [];
var myName = "John";

var i = text.indexOf(myName);

while (i !== -1) {
  hits.push(i);
  // Find index of `myName` from the index `i + 1` and store it back it in `i`
  i = text.indexOf(myName, i + 1);
}

if (hits.length === 0) {
  document.getElementById("result").innerHTML = "not found";
} else {
  document.getElementById("result").innerHTML = hits.join("<br />");
}
<pre id="result" />

Now, the while loop makes the logic more readable and clearer. Moreover, you can have enough comments inside the loop to explain the logic, even if it is a bit difficult to understand :-)

现在,while循环使逻辑更具可读性和清晰性。而且,你可以在循环内部有足够的注释来解释逻辑,即使它有点难以理解:-)

#2


Your mostly there, I'm not sure if I completely understand what you're trying to do, but your for loop isn't actually looking for the text more than once. Plus indexOf will return <0 if it finds nothing.

你大部分都在那里,我不确定我是否完全明白你想要做什么,但是你的for循环实际上不是在寻找文本多次。如果没有找到任何内容,indexOf将返回<0。

var text="asdas John asd asda sdas dasd asd Jon asdas das dasdas " +
         "dasdasdasda sadas John jsadjasd";
var hits = [];
var myName = "John";

var find = text.indexOf(myName);
while (find >= 0) {
   hits.push(text.substr(find, myName.length));
   find = text.indexOf(myName, find + 1);
}


if (hits.length === 0) {
  document.getElementById("result").innerHTML = "not found"; 
} else {
  document.getElementById("result").innerHTML = hits.toString(); 
}

#3


Try utilizing do while loop

尝试使用do while循环

var text = "asdas John asd asda sdas dasd asd Jon asdas das dasdas "        
           + "dasdasdasda sadas John jsadjasd";
    var hits = [];
    var myName = "John";
    var len = myName.length;
    var i = text.indexOf(myName);
    var result = document.getElementById("result");
    
    do {
      // push letters of `myName`
      hits.push(text[i]);
      // increment `i`
      ++i;
    } while(hits.length < len);
    
if (hits.length === 0) {
  result.innerHTML = "not found"; 
} else {
  result.innerHTML = hits.toString(); 
}
<div id="result"></div>

#4


The condition in for loop will always be true. i will always be smaller than i plus something...

for循环中的条件始终为true。我将永远小于我加上一些东西......