Well, @elclanrs already provided a link to explain String.prototype.match() . However, below is an answer that clarifies you some things.
HTML:
<img id="myImage" src="http://www.w3schools.com/js/pic_bulbon.gif" />
JavaScript:
// capture the image var image = document.getElementById('myImage'); console.log(image.src); // http://www.w3schools.com/js/pic_bulbon.gif console.log(image.src.match("word-not-in-src-name")); // null console.log(image.src.match("bulbon")); // ["bulbon", index: 32, input: "http://www.w3schools.com/js/pic_bulbon.gif"] // image.src.match("bulbon") will return an Array, but it evaluates true in JavaScript // This is the reason why "Evaluates true!" is printed out to console if(image.src.match("bulbon")) { console.log("Evaluates true!"); } // To prove my point, "Empty array!" also will be printed out to console if([]) { console.log("Empty array!"); }
You can verify this by following JS FIDDLE EXAMPLE
Therefore, to return to the sample code:
if (image.src.match("bulbon")) { image.src = "pic_bulboff.gif"; } else { image.src = "pic_bulbon.gif"; }
.. means that if the word bulbon is in the current attribute image src , the image will be changed to pic_bulboff.gif because execution will move inside the if block, because image.src.match("bulbon") will return Array (as shown in my example above is explained in docs ).
I welcome you, I hope that now you will understand how to check whether some word will be part of the line in the future :)
source share