The indexOf () method returns the position of the first occurrence of a specified string value in a string
Note:the IndexOf () method is case-sensitive! If the string value that you want to retrieve does not appear, the method returns-1.
syntax:searchvalue, required. Specifies the string value that needs to be retrieved. Fromindex, an optional integer parameter. Specifies where to start retrieving in the string. Its legal value is 0 to stringobject.length-1. If this argument is omitted, it is retrieved from the first character of the string.
Stringobject.indexof (Searchvalue,fromindex)
<script type= "Text/javascript" >var str= "Hello world!" document.write (Str.indexof ("Hello") + "<br/>") document.write (Str.indexof("World") + "< br/> ") document.write (Str.indexof (" World "))</script>
The output of the above code:
0-16
The Replace () method is used to replace other characters with some characters in a string, or to replace a substring that matches a regular expression.
syntax:regexp/substr, required. A RegExp object that specifies the substring or pattern to replace. Note that if the value is a string, it is used as the direct volume text pattern to be retrieved, instead of being converted to the RegExp object first. replacement, required. A string value. A function that specifies replacement text or generates alternate text.
Stringobject.replace (regexp/substr,replacement)
return value: A new string that was obtained after the first match or all matches of regexp were replaced with replacement .
Description: the Replace () method of the string Stringobject performs a find-and-replace operation. It looks for substrings in Stringobject that match regexp, and then replaces them with replacement . If RegExp has global flag G, then the Replace () method replaces all matching substrings. Otherwise, it replaces only the first matched substring. Replacement can be a string, or it can be a function. If it is a string, then each match is replaced by a string. However, the $ character in replacement has a specific meaning. As shown in the following table, it shows that the resulting string from a pattern match will be used for substitution.
Character |
Replace text |
$, $ 、...、 |
Text that matches the 1th to 99th sub-expression in RegExp |
$& |
Sub-string matching with RegExp |
$` |
Text located to the left of the matching substring |
$ |
Text on the right side of the matching substring |
$$ |
Direct volume symbol |
//Replace "Microsoft" in a string with "W3school"<script type= "Text/javascript" >varStr= "Visit microsoft!"document.write (Str.replace (/microsoft/, "W3school"))</script>//results: Visit w3school!//global substitution, whenever "Microsoft" is found, it is replaced with "W3school"<script type= "Text/javascript" >varStr= "Welcome to microsoft! "Str=str + "We are proud to announce that Microsoft have"Str=str + "One of the largest WEB developers sites in the world."document.write (Str.replace (/microsoft/g, "W3school"))</script>//results: Welcome to w3school! We is proud to announce that W3schoolhas one of the largest WEB developers sitesinchThe world .//ensure correct matching of string uppercase charactersvarText = "JavaScript Tutorial"; Text.replace (/javascript/i, "JavaScript");//Convert "Doe, John" to the form of "John Doe"varName = "Doe, John"; Name.replace (/(\w+) \s*, \s* (\w+)/, "$ $ $");//Replace all flower quotes with straight quotesvarName = ' "A", "B" '; Name.replace (/"([^"]*) "/g," ' $ ' ");//converts the first letter of all words in a string to uppercasevarName = ' AAA BBB CCC ';varUw=name.replace (/\b\w+\b/g,function(word) {returnWord.substring (0,1). toUpperCase () +word.substring (1);} );
The search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression.
Syntax: RegExp, which can be a substring that needs to be retrieved in Stringobject, or a RegExp object that needs to be retrieved. Note: To perform a case-insensitive retrieval, append the flag I.
Stringobject.search (RegExp)
Return value: The starting position of the first substring in stringobject that matches the regexp. Note: If no matching substrings are found, 1 is returned.
Description: The search () method does not perform a global match, and it ignores the flag G. It ignores the LastIndex property of RegExp and always retrieves from the beginning of the string, which means that it always returns the first matching position of the stringobject.
// retrieve "W3school" output: 6 <script type= "Text/javascript" >var str= "Visit w3school!" document.write (Str.search (/w3school/ </script>// Unable to retrieve W3school (because search () Case sensitive) output-1 <script type= "Text/javascript" >var str= "Visit w3school! " document.write (Str.search (/w3school/ </script>// Ignore case-sensitive retrieval output 6 <script Type= "Text/javascript" >var str= "Visit w3school!" document.write (Str.search (/w3school/i)) </script>
Reference: JavaScript indexOf () method, JavaScript Search () method, JavaScript replace () method
Js--indexof Replace Search