通过PHP在jQuery选择器中使用regex获取ID或类名 [英] Get id or class name in jquery selector use regex by PHP
问题描述
我正在尝试通过PHP在选择器中捕获所有ID和类名
I'm trying to catch all the id and class name in the selector by PHP
这是我正在使用的正则表达式:
here's the regex which I'm using:
/(?:\$|\$_|.(?:find|prependTo|closest|addClass|removeClass|toggleClass))+\(\'(.*?)\'\)/
适用于
$('#test')
$('#test .banana')
它会给我#test
和#test .banana
(我可以使用PHP按空格将它们分开)
但不能在以下选择器上使用
but it couldn't use on the following selector
$('.photo-preview[temp-id="' + Index + '"]')
$('.photo-preview[temp-id="' + Index + '"] .hello')
它只会捕获整个内容,而不仅仅是类名,
有什么方法可以捕获类名吗?
is there any way to catch the class name?
-
还没有答案,所以我决定使用这两个正则表达式,直到找到真正的答案为止
has no answer yet, so I decide use this two regex until there's a really answer
(?:\.|#)-?([_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[^\(]*\))
说明:查找所有ID和类
(?:\$|\$_|.(?:addClass|removeClass|toggleClass))\(\'([_a-zA-Z]+[_a-zA-Z0-9-].*(?=[^\(]*\'\)))
解释:在addClass(),removeClass()中找到这些类名.等等等等
因为使用addClass()时它们没有点或#符号
-
现在通过"|"将它们作为一个正则表达式混合在一起(或)
Now mix them together as one regex by "|" (OR)
这里是临时答案
(?:\.|#)-?([_a-zA-Z]+[_a-zA-Z0-9-]*)(?=[^\(]*\))|(?:\$|\$_|.(?:addClass|removeClass|toggleClass))\(\'([_a-zA-Z]+[_a-zA-Z0-9-].*(?=[^\(]*\'\)))
注意::这将捕获.和#个符号,甚至是"addClass('" 字符串,
BECAREFUL: this will catch . and # symbols, and even "addClass('" string,
因此您需要选择正确的数组,并通过PHP拆分或不必要的符号.
so you need to choose the right array, and split or the unnecessary symbols by PHP.
保持打开状态::此操作还会捕获".jpg",仍在编辑..
HOLD ON: THIS WILL CATCH ".jpg" TOO, STILL EDITING ..
推荐答案
好的,我刚刚在这里发布了另一个问题: 字符串前必须有一些东西
Okay, I just posted another question here: There must something before the string
我想我得到了答案.
当心:请注意,某些正则表达式将捕获.
和#
符号,而某些则不会捕获
BE WARE: please mind some regex will capture .
and #
symbols, and some won't
-
答案
您仍然无法在addClass()中获取类名,尽管..
you still couldn't get class name in addClass(), removeClass() though ..
/(?:(?:\$|\$_|\.(?:find|prependTo|closest))\(\'|(?<!^)\G)\s?([.#][-\w]+)?[^()\s.#]*(?=[^()]*\))/gm
-
(?:(?:\$|\$_|\.(?:find|prependTo|closest))\(\'|(?<!^)\G)
这是在选择器中确认ID或类的一个小技巧,
This is a little trick to confirm the ID or class in in a selector,
所以您不会在url(http://www.google.com/test.jpg)
中获得.jpg
,
但是坏消息是,您需要在有新选择器时手动添加它们
but the bad news is, you need to add them manually when there's a new selector
您只需要删除正则表达式中的[.#]
,因此您需要进行更改
You just need to remove [.#]
in the regex, so you need to change
/(?:(?:\$|\$_|\.(?:find|prependTo|closest))\(\'|(?<!^)\G)\s?([.#][-\w]+)?[^()\s.#]*(?=[^()]*\))/gm
^^^
Remove this bracket
-
答案
这将捕获addClass
,removeClass
和toggleClass
中的全部内容,
This will capture the whole content inside addClass
, removeClass
and toggleClass
,
据我所知,这是愚蠢的方式.
it's the dumbness way as far as I know.
/(?:\.(?:addClass|removeClass|toggleClass))\('(.*?)'\)/g
-
答案
/(?![^{]+})(?:\.|#)([_a-zA-Z]+[_a-zA-Z0-9-]*)/g
-
让我们收集第一个和第二个正则表达式
Let's collect the first and second regex
/(?:(?:\$|\$_|\.(?:find|prependTo|closest))\(\'|(?<!^)\G)\s?([-\w]+)?[^()\s.#]*(?=[^()]*\))|(?:\.(?:addClass|removeClass|toggleClass))\('(.*)'\)/g
因此您现在可以在选择器中获取ID和类
so you can now get IDs and classes in selectors,
,您仍然需要以其他方式在addClass()和removeClass()中拆分类.
and you still need to split classes in addClass(), removeClass() in other way.
这篇关于通过PHP在jQuery选择器中使用regex获取ID或类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!