= 0) Note that this method is case sensitive. If you need a case-insensitive search, you can write this: if (str.toLowerCase().indexOf("yes") >= 0) // or if (/yes/i.test(str)) // or // You can use search or match for this. str."/> = 0) Note that this method is case sensitive. If you need a case-insensitive search, you can write this: if (str.toLowerCase().indexOf("yes") >= 0) // or if (/yes/i.test(str)) // or // You can use search or match for this. str.">

Home >Web Front-end >JS Tutorial >jQuery String Contains Functions

jQuery String Contains Functions

Christopher Nolan
Christopher NolanOriginal
2025-03-10 00:13:09976browse

jQuery String Contains Functions

No jQuery is required, just use pure JavaScript to determine whether the string contains another string! The following is the operation method:

if (str.indexOf("Yes") >= 0)

Please note that this method is case sensitive. If you need a case-insensitive search, you can write this:

if (str.toLowerCase().indexOf("yes") >= 0)
// 或
if (/yes/i.test(str))
// 或
// 您可以为此使用 search 或 match。
str.search('Yes')

Another example of checking if a string contains another string:

if (v.indexOf("http:") == -1) {
  // 字符串不包含 http
}

This will return the position of the match and -1 if not found.

FAQs about jQuery string functions (FAQ)

How to check if a string in jQuery contains a specific substring?

In jQuery, you can use the indexOf() method to check if a string contains a specific substring. This method returns the index position in the string where the specified value first appears. If the value is not found, return -1. Examples are as follows:

var str = "Hello world!";
var n = str.indexOf("world");

In this example, n will be 6, because "world" starts at the 6th position of the string "Hello world!".

What is the difference between

and indexOf() in jQuery? lastIndexOf()

and indexOf() are both used to find the position of the substring in the string. The difference is in the search direction. lastIndexOf() Start a search from the beginning of the string and return to the location where the specified substring first appears. On the other hand, indexOf() starts searching from the end of the string and returns the location where the specified substring last appeared. lastIndexOf()

How to replace substrings in strings with jQuery?

You can replace substrings in strings using the

method in jQuery. This method searches for the string for the specified value or regular expression and returns a new string where the specified value has been replaced. Examples are as follows: replace()

var str = "Hello world!";
var newStr = str.replace("world", "jQuery");
In this example,

will be "Hello jQuery!". newStr

How to convert a string in jQuery to lowercase or uppercase?

You can convert strings to lowercase or uppercase using the

and toLowerCase() methods in jQuery. Examples are as follows: toUpperCase()

var str = "Hello World!";
var lowerCaseStr = str.toLowerCase();
var upperCaseStr = str.toUpperCase();
In this example,

will be "hello world!" and lowerCaseStr will be "HELLO WORLD!". upperCaseStr

How to split a string in jQuery into a substring array?

You can split the string into a substring array using the

method in jQuery. This method splits the string into a substring array based on the specified splitter. Examples are as follows: split()

var str = "Hello world!";
var arr = str.split(" ");
In this example,

will be [arr, Hello]. world!

How to concatenate arrays of strings in jQuery into single strings?

You can concatenate arrays of strings into a single string using the

method in jQuery. This method concatenates all elements of the array into a string. Examples are as follows: join()

if (str.indexOf("Yes") >= 0)

In this example, str will be "Hello world!".

How to extract part of a string in jQuery?

You can use the substring() method in jQuery to extract part of a string. This method extracts the characters between two specified indexes from the string and returns a new substring. Examples are as follows:

if (str.toLowerCase().indexOf("yes") >= 0)
// 或
if (/yes/i.test(str))
// 或
// 您可以为此使用 search 或 match。
str.search('Yes')

In this example, subStr will be "Hello".

How to delete spaces at both ends of strings in jQuery?

You can use the trim() method in jQuery to delete spaces at both ends of a string. This method does not change the original string. Examples are as follows:

if (v.indexOf("http:") == -1) {
  // 字符串不包含 http
}

In this example, trimmedStr will be "Hello world!".

How to find the length of a string in jQuery?

You can use the length property in jQuery to find the length of a string. This property returns the number of characters in the string. Examples are as follows:

var str = "Hello world!";
var n = str.indexOf("world");

In this example, len will be 12.

How to connect two strings in jQuery?

You can concatenate two strings using the concat() method in jQuery. This method concatenates two or more strings into one string. Examples are as follows:

var str = "Hello world!";
var newStr = str.replace("world", "jQuery");

In this example, str will be "Hello world!".

The above is the detailed content of jQuery String Contains Functions. 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