7/7 Fine tune the syntax


#1

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


#2

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++) {
            hits.push(text[j]);
        }
        i += k-1;
    }
}

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


#3

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++){
            hits.push(text[j]);
        }
    }
}
if(hits.length === 0){
    console.log( "Your name wasn't found!");
}
else{
    console.log(hits);
}

#4

@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');
text.match(re);
console.log(text.match(re));
console.log(text.match(re).length);

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.


#5

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!


#6

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.


#7

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


#8

Thanks! An easy fix.


#9

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


#10

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.