= 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
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.
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!".
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()
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
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
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!
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!".
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".
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!".
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.
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!