xFactorSchool.com(Beta)
How to Do Examples
Python Examples
HTML Examples
Code Examples
C Code Examples
C++ Code Examples
C# Code Examples
Java Code Examples
Python Code Examples
HTML Code Examples
CSS Code Examples
JavaScript Code Examples
Online Editor
C Code Editor
C++ Code Editor
C# Code Editor
Java Code Editor
Python Code Editor
HTML Code Editor
CSS Code Editor
JavaScript Code Editor
Practice how to return an iterator of a specified text in a String in JavaScript
JavaScript Online Editor
Run Code
More JavaScript Sample Codes
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Return an iterator of a specified text in a string in JavaScript</title> <!--Script tag is added to write JavaScript code inside it--> <script> let myString = "People loving fun use JavaScript to have fun over fun."; let iteratorVar = myString.matchAll("fun"); //matchAll() method is is used to return an iterator of the matching text let iteratorReg = myString.matchAll(/fun/g); //Regular expression can be used to return an iterator of all matched text var strlength = 0; // get length of iterator for( const x of iteratorReg) { strlength++; } function myFunction() { document.getElementById("outPut").innerHTML = `The iterator of fun from the string is: ${Array.from(iteratorVar)}.`; document.getElementById("outPutReg").innerHTML = `The fun occurs ${strlength} time in the string.`; } </script> </head> <body> <h1>How to Return an Iterator of a Specified Text in a String in JavaScript?</h1> <button onclick="myFunction()">Click here to see result</button> <p id="outPut"></p> <p id="outPutReg"></p> </body> </html>