Home >Web Front-end >JS Tutorial >jquery determines whether it contains the specified string

jquery determines whether it contains the specified string

藏色散人
藏色散人Original
2021-01-12 08:56:0412564browse

Jquery method to determine whether a specified string is included: 1. Use the indexOf() and lastIndexOf() methods to determine whether a string contains the specified string; 2. Use the test() method to determine whether a string Match a pattern.

jquery determines whether it contains the specified string

The operating environment of this tutorial: windows7 system, jquery1.10.0 version, Dell G3 computer.

Recommended: jQuery video tutorial

Method 1: Use indexOf() and lastIndexOf() methods

Case:

var Cts = "bblText"; 
if(Cts.indexOf("Text") >= 0 ) { 
    alert('Cts中包含Text字符串'); 
}

indexOf Usage:

Returns the character position of the first occurrence of a substring in the String object.

strObj.indexOf(subString[, startIndex])

Parameters

strObj: required. String object or literal.

subString: required. The substring to find in the String object.

starIndex: optional. This integer value indicates the index within the String object at which to begin the search. If omitted, the search is from the beginning of the string.

Description

The indexOf method returns an integer value indicating the starting position of the substring within the String object. If the substring is not found, -1 is returned.

If startindex is a negative number, startindex is treated as zero. If it is greater than the largest character position index, it is treated as the largest possible index.

Perform the search from left to right. Otherwise, the method is the same as lastIndexOf.

Note:

indexOf() method is case-sensitive!

If the string value to be retrieved does not appear, this method returns -1.

The usage of lastIndexOf() is the same as indexOf(), except that it searches from the right to the left.

Method 2: Use test() method

Example: In the following example, we will retrieve "W3School":

var str = "Visit W3School";
var patt1 = new RegExp("W3School");
var result = patt1.test(str);
document.write("Result: " + result);

Result output :

Result: true

test() method

test() method is used to detect whether a string matches a certain pattern.

Syntax

RegExpObject.test(string)

Parameters

string Required. The string to detect.

Return value

If the string string contains text that matches RegExpObject, it returns true, otherwise it returns false.

Explanation

Call the test() method of the RegExp object r and pass it the string s, which is equivalent to this expression: (r.exec(s) != null ).

The above is the detailed content of jquery determines whether it contains the specified string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn