Guide
1
Use the indexOf to organize the search substring in a string variable in programming scripting language JavaScript.This feature provides the use of two parameters, one of which is a substring of the desired is mandatory.Another parameter may indicate the index symbol in a string variable from which to start the search substring - this parameter is optional and defaults to zero.According to the syntax rules of the language of the original string variable must be written before the function and separated from it by a point.For example: "The original string" .indexOf ("st
ring", 2) function returns the index of the first encountered her occurrence of the specified substring in the original string.In this example, it returns 9. If the occurrence is not found, the indexOf returns -1.Note that this function in the search case sensitive.
2
lastIndexOf function is used to search for occurrences of a substring in the opposite direction, that is, starting with the last character of the original string value.Syntax lastIndexOf virtually no differences from the above functions - it is also possible to pass two parameters, one of which (the desired substring) is mandatory.The second parameter of this function can indicate the start position of the search and should be counted in the direction from the first to the last character.This feature is also case-sensitive when searching, and returns -1 if the match was not found.Sample: "The original string" .lastIndexOf ("string", 2) This function returns a value of -1, as the search will start from the second position by the end of the string, which will exclude the presence of the text entirely.
3
function Use Search to search occurrence of a string with a regular expression (regexp).This feature requires only one parameter - the regular expression.The rest of the syntax and return values ​​coincide with the rules for the previous functions.Sample: "The original string" .search (/ string / i) This example also returns the value 9. Of course, the use of regular expressions allows a much more fine-tuning the search, but also require the use of much larger resources of the system, what should not be forgotten when programmingenough intensive scripts.