字符串前必须有东西 [英] There must something before the string
问题描述
我正在使用PHP并尝试捕获所有ID& jQuery选择器中的类,例如:
I'm using PHP and trying to catch all the IDs & classes in jQuery's selector like:
$('#ddd .aaa #bbb #c').show();
test.$('#eee[type=submit] .fff').addClass('test')
but my regex will catch '.jpg', '.png' .. url too, now I have another idea
任何类名或ID前面都必须有一个$('
符号"
"there must a $('
symbol in front of any class name or id"
(因此它不会捕获任何URL链接或十六进制代码)
-
\$\('((?:\.|#)([_a-zA-Z]+[_a-zA-Z0-9-]*))(?=[^\(]*\))
但只会抓到第一场比赛
#ddd
#eee
如何获取每个类的名称或ID,如:
how can I get each classes name or IDs like:
#ddd
.aaa
#bbb
#c
#eee
.fff
并保留(或重复?)规则:字符串前面必须有$('
个符号.
and keep(or repeat?) the rule: there must a $('
symbols in front of the string.
-
答案:
此正则表达式将搜索$()
,.find()
,.prependTo()
,.closest()
,
This regex will search the classes and IDs in $()
, .find()
, .prependTo()
, .closest()
,
所以您需要手动添加更多选择器:
so you need to add more selector manually:
/(?:(?:\$|\$_|\.(?:find|prependTo|closest))\(\'|(?<!^)\G)\s?([.#][-\w]+)?[^()\s.#]*(?=[^()]*\))/gm
推荐答案
您需要使用 \G
在上一个匹配结束的位置匹配.在第一次匹配尝试期间,\G
会像\A
一样在字符串的开头进行匹配.
You need to use \G
anchor here. The anchor \G
matches at the position where the previous match ended. During the first match attempt, \G
matches at the start of the string in the way \A
does.
(?:\$\('|(?<!^)\G)\s?([.#]\w+)[^()\s.#]*(?=[^()]*\))
这篇关于字符串前必须有东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!