Home > Web Front-end > JS Tutorial > body text

How to check if a string contains specified characters in jquery?

青灯夜游
Release: 2020-11-17 13:53:30
Original
2937 people have browsed it

Method: Use indexOf() or lastIndexOf() to check, both of which can return the position of the specified character in the string. If the string does not contain the specified character, "-1" is returned; so just Just judge whether the return value is greater than or equal to, for example "if (string.indexOf(character)>= 0){}".

How to check if a string contains specified characters in jquery?

[Related recommendations: jQuery video tutorial]

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

When you cannot determine whether a character actually exists in a string, you can call the indexOf() and lastIndexOf() methods.

The indexOf() and lastIndexOf() methods return the position of the specified substring in another string. If the substring is not found, -1 is returned.

The difference between these two methods is that the indexOf() method starts to retrieve the string from the beginning of the string (position 0), while the lastIndexOf() method starts to retrieve the substring from the end of the string. string.

Case:

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

indexOf Usage:

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

strObj.indexOf(subString[, startIndex])
Copy after login

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 negative, 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: The

  • 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 the test() method

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

var str = "hello world";
var patt1 = new RegExp("world");
var result = patt1.test(str);
document.write("Result: " + result);
Copy after login

Result output:

Result: true
Copy after login

test() usage introduction:

test() method is used Check whether a string matches a 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

  • Calling the test() method of the RegExp object r and passing it the string s is equivalent to this expression of: (r.exec(s) != null).

For more programming-related knowledge, please visit:

Programming Learning! !

The above is the detailed content of How to check if a string contains specified characters in jquery?. 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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!