revision:
The search() method matches a string against a regular expression and returns the index (position) of the first match. It returns -1 if no match is found. The search() method is case sensitive.
If the search value is a string, it is converted to a regular expression.
string.search(searchValue)
Parameters:
searchvalue : required. The search value. A regular expression (or a string that will be converted to a regular expression).
<p>search() searches a string for a value and returns the position of the first match:</p> <p id="demo"></p> <script> let text = "Mr. Blue has a blue house" let position = text.search("Blue"); document.getElementById("demo").innerHTML = position; </script>
<div> <p id="search-1"></p> <p id="search-2"></p> <p id="search-3"></p> <p id="search-4"></p> <p id="search-5"></p> <p id="search-6"></p> </div> <script> let text = "Mr. Blue has a blue house" document.getElementById("search-1").innerHTML = "sentence : " + text; let position = text.search("Blue"); document.getElementById("search-2").innerHTML = "position 'Blue' : " + position; let position2 = text.search("blue"); document.getElementById("search-3").innerHTML = "position 'blue' : " + position2; let position3 = text.search(/Blue/); document.getElementById("search-4").innerHTML = "position '/Blue/' : " + position3; let position4 = text.search(/blue/); document.getElementById("search-5").innerHTML = "position '/blue/' : " + position4; let position5= text.search(/blue/i); document.getElementById("search-6").innerHTML = "position '/blue/i' : " + position5; </script>