7/7 Fine tune the syntax


Hi, does anyone know how to fine tune this exercise to find the exact string needed? Thanks in advance.


Care to try some forward referencing?

var text, myName, hits;
var i, j, k, l, m;
text = "people places things jerbalaskit assault JoshuaJoshua franciso Jay san francisco execution blah blah blah";
myName= "Joshua";
m = myName.length;
hits = [];

for (i = 0; i < text.length; i++) {
    if (text[i] === myName[0]) {
        l = 0;
        for (k = 0; k < m; k++) {
            l += text[i+k] === myName[k] ? 1 : 0;
        if (l < m) continue;
        for (j = i; j < i + m; j++) {
        i += k-1;

Granted, it is still letter by letter as the exercise permits. It does shed new light, though, eh?


HI, how about this ?

/*jshint multistr:true */

var text = "Eric xzEricczxc Eric zxkjclcz mclkzxmcz Edie czxlkcm cmzxklc zz x Ericc";
var myName = "Eric";
var hits = [];
for(i=0; i < text.length; i++){
    if( text[i] === "E" && text[i+1] === "r" && text[i+2] === "i" && text[i+3] === "c" && text[i+4] === " "){
        for(j=i; j < myName.length + i; j++){
if(hits.length === 0){
    console.log( "Your name wasn't found!");


@pypro18202 @objectslayer61602

This is the best search engine I managed to write after searching & reading on the internet for over 6 hours:

text = "people places things jerbalaskit assault JoshuaJoshua franciso Jay san francisco execution joshua J oshua adefdgdfgv@:Dfd;f'>?1341234234 adefdgdgd gregt erfgh JOSHUA Joshua blah blah blah";
var myName = "Joshua";
var re = new RegExp(myName, 'g');

It uses a completely different approach - the RegExp constructor to create a regular expression object for matching text with a pattern. In this case it matches whatever string you put as myName against the block of characters.

This engine is very effective for finding precisely what you are looking for as it will create an array of the string you are searching for and will also count how many times it positively matches. It is really brutal and will even ignore anything that isn't an exact match with myName for example joshua, J0shua, J oshua, JOSHUA...

Have a go and test it - I have added some special characters and decoys in "text".

Hope that helps.


Without a doubt, regular expressions are one of the most powerful parsing tools at our disposal. Their use is universal across the programming globe. Always fun to get a chance to play with them.

var matchTest = text.match(re);

One thing that we don't have from this is the indices. With only an array of matches and a count, we are still not able to locate them in the string. We have confirmed their presence, mind, so it is a good first step before running a script to locate them.

if (matchTest.length) {
    // locate the matches and compile their indices
} else {
    console.log("Your name was not found.");

Once located, we can wrap the name in a <span></span> element and give the text some highlighting. One idea, anyway. Happy exploring!


implies that we have an index tool, which we do... indexOf and for our purposes, here, lastIndexOf. Their names should be a dead giveaway.

Whether or not we can confirm a match by some other means, these tools are smack on matches or nothing doing. And they give us information we can use.

'for our purposes' we need to course through an object from the end, not the beginning, shortening the object with each discovered match, while at each match returning an index. Bing, bang, bong. We have all the index positions and can give our program the data it needs to manipulate the HTML.


this was a simple easy fix for mine!
thanks a lot!


Thanks! An easy fix.


Could anyone please explain how to use the RegExp function and apply it in our program. Thank you in advance


Not in this topic thread. It is taken. You could post this question as a new topic. If you would, please, that would be great. Thank you.