<script type="text/javascript"> var txt = "Hello World!"; var emailAddress= "JimSuo2@rlay.com"; //The indexOf() method returns the position of the requested character. document.write("The indexOf() method returns the position of the requested character." + "<br />"); var pos_at=emailAddress.indexOf("@"); var pos_dot=emailAddress.indexOf("."); var pos_o=emailAddress.lastIndexOf("o"); document.write("The length of the string, txt: " + txt.length + "<br />");//The length of the string, txt. document.write("The lenght of the email address: "+ emailAddress.length + "<br />");//The length of the email address. document.write("The position of @: " + pos_at + "<br />"); document.write("This is the 8th character of the string, since the index count starts at 0
and spaces are counted as a character." + "<br />"); document.write("The position of the dot: " + pos_dot + "<br />"); document.write("The position of the 'o' with the search working backwards: " + pos_o + "<br />"); </script>
The JavaScript lastIndexOf() Method starts its position search in the opposite direction of the indexOf() method. JavaScript lastIndexOf is case sensitive so: the I and O have to be capitalized in its name. In effect, this method returns the position of the last found occurrence of the specified value in the string. In summary, the lastIndexOf() Method: