Javascript method to determine whether a string contains a certain string: 1. Use the [indexOf()] method to return the position where a specified string value first appears in the string; 2. Use The [search()] method is used to retrieve a specified substring in a string.
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
Method 2: search()
var str = "123"; console.log(str.search("3") != -1 ); // true
Method 3: match()
var str = "123"; var reg = RegExp(/3/); if(str.match(reg)){ // 包含 }
Method 4: test()
var str = "123"; var reg = RegExp(/3/); console.log(reg.test(str)); // true
Method 5: exec()
var str = "123"; var reg = RegExp(/3/); if(reg.exec(str)){ // 包含 }
Related free learning recommendations: javascript(Video)
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!