jQuery 匹配正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9882693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery match regex
提问by Greg
var playerClass = $(this).parent().attr('class')
Question about only selecting a specific class from the dom.
关于仅从 dom 中选择特定类的问题。
What can I add to this line to only select the class that has a class of player-1, player-2, basically anything with a prefix of player- followed by a number?
我可以向这一行添加什么以仅选择具有 player-1、player-2 类的类,基本上任何前缀为 player- 后跟一个数字的类?
回答by jfriend00
I couldn't tell exactly what you were asking with your question, so here are four possibilities. Hopefully one of these matches what you intended to ask or you can slightly modify one of them to fit.
我无法确切地说出你问的问题,所以这里有四种可能性。希望其中之一符合您的要求,或者您可以稍微修改其中之一以适应。
To select all input items with a class of player-xx
where xx
is a number, you can do this:
要选择player-xx
where xx
is a number类的所有输入项,您可以执行以下操作:
var playerClass = $('input[class^="player-"]').filter(function() {
return((" " + this.className + " ").match(/\splayer-\d+\s/) != null);
});
The first jQuery selector gets any class name that starts with "player-"
. The filter then eliminates any items that aren't also "player-nn"
.
第一个 jQuery 选择器获取任何以"player-"
. 然后过滤器会消除所有不是 的项目"player-nn"
。
If you're only trying to examine whether one particular classname contains player-nn
, then you can do that like this:
如果您只是想检查一个特定的类名是否包含player-nn
,那么您可以这样做:
if ((" " + $(this).parent().attr('class') + " ").match(/\splayer-\d+\s/) != null) {
// the parent has player-nn as a class
}
If you just want to get the matching class on the parent, you can do that with this:
如果您只想在父级上获得匹配的类,您可以这样做:
var matches = (" " + $(this).parent().attr('class') + " ").match(/\s(player-\d+)\s/);
if (matches) {
playerClass = matches[1];
}
If you're trying to actually get the number after the player-
, you can do that like this:
如果您想实际获取 之后的数字player-
,您可以这样做:
var matches = (" " + $(this).parent().attr('class') + " ").match(/\splayer-(\d+)\s/);
if (matches) {
playerNum = parseInt(matches[1], 10);
}
Note: All of these examples use a trick (which I learned from jQuery) that if you add a space onto the beginning and end of the overall classname, then you can look for a given classname with a regex by looking for your particular classname surround on both sides by whitespace. This makes sure you don't match a part of a longer classname and only match whole classnames. jQuery uses this trick in it's .hasClass()
implementation.
注意:所有这些示例都使用了一个技巧(这是我从 jQuery 中学到的),如果您在整个类名的开头和结尾添加一个空格,那么您可以通过查找特定类名环绕来查找带有正则表达式的给定类名两边都是空格。这可确保您不匹配较长类名的一部分,而只匹配整个类名。jQuery 在它的.hasClass()
实现中使用了这个技巧。
回答by elclanrs
I think this is what you need:
我认为这就是你需要的:
var playerClass = $(this).parent().attr('class').match(/player-\d+/);
回答by Marc
To select all input
elements that have a class name starting with "player-", you could use this:
要选择input
类名以“player-”开头的所有元素,您可以使用以下命令:
var playerClass = $('input[class^="player-"]');
The "starts-with" selector is described here: http://api.jquery.com/attribute-starts-with-selector/
这里描述了“开始”选择器:http: //api.jquery.com/attribute-starts-with-selector/
This will not ensure that "player-" is immediately followed by a digit though.
但这并不能确保“player-”后面紧跟一个数字。
回答by icyrock.com
You can use jQuery regex selector:
您可以使用 jQuery 正则表达式选择器: