Home>Article>Web Front-end> How to determine whether a string contains specific characters in jquery

How to determine whether a string contains specific characters in jquery

藏色散人
藏色散人 Original
2021-01-20 10:16:15 13647browse

Jquery method to determine whether a string contains specific characters: 1. Use the indexOf() and lastIndexOf() methods; 2. Use the test() method to determine whether a string contains specific characters. The syntax is such as "RegExpObject. test(string)".

How to determine whether a string contains specific characters in jquery

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

Recommended tutorial:jq tutorial

Jquery method to determine whether a string contains specific characters

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

Case:

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

indexOf usage:

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

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

Usage introduction:

Definition and usage

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

Syntax

RegExpObject.test(string)

Parameter Description

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 How to determine whether a string contains specific characters in jquery. 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