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

jquery determines whether it contains the specified string

藏色散人
Release: 2022-12-30 11:11:31
Original
12587 people have browsed it

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字符串'); 
}
Copy after login

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);
Copy after login

Result output :

Result: true
Copy after login

test() method

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

Syntax

RegExpObject.test(string)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template