6/7 Victory! Additional Task


#1

The task: "Think about how you might fine-tune this program to make sure it only finds exact matches for your name. Search the Internet to see if there are any built-in JavaScript string methods that can help!"

Well, I made this:

/*jshint multistr:true */

var text = "This is a simple text, John. Yes, John! \
Text, just a simple text, my dear Jonh.";
var myName = "John";
var re = new RegExp(myName, 'g');
var search = text.match(re);

if (search < 1) {
    console.log("Your name wasn't found!");
}
else {
    s = search.length;
    console.log("Found " + s + " occurence(s)");
    console.log(search.join(' '));
}

It works perfectly. The question is should I alter the code using RegExp (which I did) or somehow implement the RegExp into the code from the previous exercise?

Thank you!


#2

Are you asking if there is another way without regular expressions?