5

I search for the word "i" in the text, but it only shows the first "i" and does not show the rest of "i" I want you to show me the whole text though Help me please

const searchItem = () => {
  const source = "Lorem ipsum dolor sit amet consectetur adipisicing elit."
  const searchDate = "i";
  for (let i = 0; i < source.length; i++) {
    let res = source.search(searchDate);
    if (res > 0) {
      document.getElementById("demo").innerHTML = res;
    } else if (res < 0) {
      document.getElementById("demo").innerHTML = "No results found";
    }
  }
}
<button onclick="searchItem()">Try it</button>

<p id="demo"></p>
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Amir
  • 63
  • 5
  • https://stackoverflow.com/a/4009768/1461850 – Lee Jan 05 '21 at 14:07
  • 5
    What is the expected result? – Unmitigated Jan 05 '21 at 14:08
  • 2
    Your call `source.search(searchDate)` is always the same, it does not depend in any way on the value of `i`. The `for` loop has no effect on the `search` call, it is just repeated a lot of times, and every time it runs in exactly the same manner. – Peter B Jan 05 '21 at 14:10
  • What you want to return? currently you return the number of "i" occurrences it is 6 (not 7) because it is 0 indexed – Ben.S Jan 05 '21 at 14:11
  • 1
    @iota In the above sentence, there is more than one i. I want to show me the index of all available i, for example, the result "6, 19" means the places where i exists, show their index – Amir Jan 05 '21 at 14:12
  • Does this answer your question? [How to count string occurrence in string?](https://stackoverflow.com/questions/4009756/how-to-count-string-occurrence-in-string) – MajiD Jan 05 '21 at 14:13
  • If you just want to count the occurences instead of writing this much code u can simply use split method to count the letter occurrences – Ronak07 Jan 05 '21 at 14:16
  • Does this answer your question? [Search for all instances of a string inside a string](https://stackoverflow.com/questions/3365902/search-for-all-instances-of-a-string-inside-a-string) – Sunil Chaudhary Jan 05 '21 at 15:38

6 Answers6

3

You can use String#indexOf with the fromIndex argument to continuously search for the string.

const searchItem = () => {
  const source = "Lorem ipsum dolor sit amet consectetur adipisicing elit."
  const searchDate = "i";
  const indexes = [];
  let i, prev = 0;
  while((i = source.indexOf(searchDate, prev)) !== -1){
    indexes.push(i);
    prev = i + searchDate.length;
  }
  if (indexes.length > 0) {
    document.getElementById("demo").innerHTML = indexes;
  } else {
    document.getElementById("demo").innerHTML = "No results found";
  }
}
<button onclick="searchItem()">Try it</button>

<p id="demo"></p>
0stone0
  • 34,288
  • 4
  • 39
  • 64
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
1

.search() returns the index of the first match. It does not search the whole string.


Since the for loop in the code isn't used, You're probably trying use that loop to compare each char in the string;

const searchItem = () => {
    const source = "Lorem ipsum dolor sit amet consectetur adipisicing elit."
    const searchDate = "i";
    let res = 0;

    // For each character in source
    for (let i = 0; i < source.length; i++) {
        
        // If current char matches searchDate
        if (source[i] === searchDate) {
        
            // Bumb result counter
            res++;
        }
    }

    // After looping through all the chars, show result based on res
    if (res > 0) {
        document.getElementById("demo").innerHTML = res;
    } else if (res < 0) {
        document.getElementById("demo").innerHTML = "No results found";
    }
}
<button onclick="searchItem()">Try it</button>

<p id="demo"></p>
0stone0
  • 34,288
  • 4
  • 39
  • 64
1

const searchItem = () => {
  const str = "Lorem ipsum dolor sit amet consectetur adipisicing elit."
  const res = str.split("i").length - 1;
  if (res > 0) {
    document.getElementById("demo").textContent = res;
  } else {
    document.getElementById("demo").textContent = "Invalid";
  }
}
<button onclick="searchItem()">Try it</button>

<p id="demo"></p>

If your aim is to only find the occurrences of letters in word then that can be done by simple split method.

const str = "Lorem ipsum dolor sit amet consectetur adipisicing elit.";
const result = str.split("i").length - 1;
console.log(result);
Ronak07
  • 894
  • 5
  • 26
1

Seems to me like you're trying to count the occurrences within a string, this should do:

var findOccurrences = () => {
   var searchQuery = document.getElementById('searchQuery').value;
   var source = document.getElementById('source').value;

   var regExp = new RegExp(searchQuery, 'g');
    
   var match = source.match(regExp);
   var occurrences = 0;
   
   if(match) {
    occurrences = match.length;
   }

   document.getElementById('demo').innerHTML = occurrences > 0 ? occurrences : "No results found";
 }
<input id="source" value="Lorem ipsum dolor sit amet consectetur adipisicing elit."/>
<input id="searchQuery" />
<button onclick="findOccurrences()">Try it</button>
<div id="demo"></div>
Bargros
  • 521
  • 6
  • 18
0

another way. get the length of the string. replace all occurrences and get the length again, the difference is your result.

const searchItem = () => {
      const source = "Lorem ipsum dolor sit amet consectetur adipisicing elit."
      const searchDate = "i";
      const initialLen = source.length;
      const replacedLen = source.split(searchDate).join("").length;
      const diff = initialLen - replacedLen;
      if(diff > 0){
        document.getElementById("demo").innerHTML = diff
      }
      else{
        document.getElementById("demo").innerHTML = "No results found";
      }
}
<button onclick="searchItem()">Try it</button>

<p id="demo"></p>
mehmetx
  • 850
  • 6
  • 6
0
<script>
    const searchItem = () => {
        const source = "Lorem ipsum dolor sit amet consectetur adipisicing elit."
        const searchDate = "i";
        const demo = document.getElementById("demo");
        for (let i = 0; i < source.length; i++)
            if (source[i] === searchDate)
                demo.innerHTML += i + ' ';
        if (demo.innerHTML === '')
            demo.innerHTML = "No results found";
    }
</script>
Vahid
  • 655
  • 4
  • 11