Home >Web Front-end >JS Tutorial >How to determine whether a string contains a certain string in javascript
Javascript method to determine whether a string contains a certain string: 1. Use the [indexOf()] method; 2. Use the [search()] method; 3. Use the [match()] method.
The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.
Javascript method to determine whether a string contains a certain string:
Method 1: indexOf()
var str = "123"; console.log(str.indexOf("3") != -1 ); // true## The #indexOf() method returns the position of the first occurrence of a specified string value in a string. If the string value to be retrieved does not appear, the method returns -1.
Method 2: search()
var str = "123"; console.log(str.search("3") != -1 ); // trueThe search() method is used to retrieve the specified substring in the string, or to retrieve the substring that matches the regular expression. substring. If no matching substring is found, -1 is returned.
Method 3: match()
var str = "123"; var reg = RegExp(/3/); if(str.match(reg)){ // 包含 }The match() method can retrieve a specified value within a string, or find a match for one or more regular expressions .
Related free learning recommendations:
The above is the detailed content of How to determine whether a string contains a certain string in javascript. For more information, please follow other related articles on the PHP Chinese website!