javascript determines whether a string contains a string

Here Insert Picture Description

Method One: indexOf () (recommended)

STR = var "123";
the console.log (str.indexOf ( ". 3") = -1!); // to true
the indexOf () method returns a string value of the specified position of the first occurrence of the string. If the string value to be retrieved is not present, then the method returns -1.


Method two: search ()

STR = var "123";
the console.log (str.search ( ". 3") = -1!); // to true
Search () method is used substring search character string specified in the regular expression or retrieve sub-string matches. If no matches any substring, or -1.


Method three: match ()

STR = var "123";
var REG = the RegExp (/. 3 /);
IF {((REG) str.match)
// contains
}
specified value match () method within the string may be retrieved, or to find one or more regular expression matching.


Method four: test ()

STR = var "123";
var REG = the RegExp (/. 3 /);
// to true; the console.log (reg.test (STR))
values specified search character string test () method is used. Returns true or false.


Method five: exec ()

STR = var "123";
var REG = the RegExp (/. 3 /);
IF (reg.exec (STR)) {
// comprising
}
Exec () method is used to retrieve the string matching the regular expression. It returns an array in which to store the matching results. If a match is not found, the return value is null.


Reproduced original: www .cnblogs.com / ooo0 / p /7741651.html

Here Insert Picture Description

Method One: indexOf () (recommended)

STR = var "123";
the console.log (str.indexOf ( ". 3") = -1!); // to true
the indexOf () method returns a string value of the specified position of the first occurrence of the string. If the string value to be retrieved is not present, then the method returns -1.


Method two: search ()

STR = var "123";
the console.log (str.search ( ". 3") = -1!); // to true
Search () method is used substring search character string specified in the regular expression or retrieve sub-string matches. If no matches any substring, or -1.


Method three: match ()

STR = var "123";
var REG = the RegExp (/. 3 /);
IF {((REG) str.match)
// contains
}
specified value match () method within the string may be retrieved, or to find one or more regular expression matching.


Method four: test ()

STR = var "123";
var REG = the RegExp (/. 3 /);
// to true; the console.log (reg.test (STR))
values specified search character string test () method is used. Returns true or false.


Method five: exec ()

STR = var "123";
var REG = the RegExp (/. 3 /);
IF (reg.exec (STR)) {
// comprising
}
Exec () method is used to retrieve the string matching the regular expression. It returns an array in which to store the matching results. If a match is not found, the return value is null.


Reproduced original: www .cnblogs.com / ooo0 / p /7741651.html

Guess you like

Origin blog.csdn.net/abcdef12030/article/details/92066166