简体   繁体   English

用Jquery查找子字符串?

[英]Find substring with Jquery?

How would our group find out if a particular string contains a certain substring? 我们的小组如何确定特定字符串是否包含某个子字符串? Only with the help of , please. 请在的帮助下。

You don't really need jQuery for such a simple thing, you can simply use the indexOf method of String objects, eg: 你不需要jQuery来做这么简单的事情,你可以简单地使用String对象的indexOf方法,例如:

var str = "foobar";
var containsFoo = str.indexOf('foo') >= 0; // true

The indexOf method returns the character index where the first occurrence of the specified value is encountered, if not found, it returns -1 . indexOf方法返回遇到指定值第一次出现的字符索引,如果未找到,则返回-1

Why use 10 characters when 100 will do? 为什么100会使用10个字符?

Here's the requested jQuery plugin: 这是请求的jQuery插件:

jQuery.isSubstring = function(haystack, needle) {
    return haystack.indexOf(needle) !== -1;
};

Usage: 用法:

$.isSubstring("hello world", "world")); // true;​​​​​​​​​​​

If your limited to jQuery which is just JavaScript... you can use the filter 如果你的jQuery只限于JavaScript ...你可以使用过滤器

var subgtSel = $("#jquerySelector").filter(function(i) {
    // do your filter here
    return $(this).attr("data-timestamp") <= subMsg.CreateDateTimeStamp;
});

the subgtSel becomes your new jQuery now with the relevant filter in the above. subgtSel现在成为你的新jQuery,上面有相关的过滤器。 In the above, I am looking for all div elements that have an attribute that is less than the subMsg.CreateTimeStamp. 在上面,我正在寻找属性小于subMsg.CreateTimeStamp的所有div元素。

If your looking for a particular substring... you can do the following with jQuery right in the selector 如果您正在寻找特定的子字符串...您可以在选择器中使用jQuery执行以下操作

var sel = $("#jquerySelector:contains('text')");

see http://api.jquery.com/contains-selector/ http://api.jquery.com/contains-selector/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM