正则表达式捕获特定单词后的大括号
我想捕获特定选择器之后大括号之间的字符串。例如,我有这样的字符串:
<div id="text-module-container-eb7272147" class="text-module-container"><style>#123-module-container-eb7272147 p{text-color:#211E22;bgcolor:test;} #text-module-container-eb7272147 p{color:#211E1E;} #text-module-container-eb7272147 p{color:#123444;} </style>
现在,如果我给选择器 #123-module-container-eb7272147 p
它应该返回 text-color:#211E22;bgcolor:test;
我可以获取大括号之间的数据,但不能使用特定的选择器。这是尝试过的代码 https://regex101.com/r/AESL8q/1
I wanted to capture the string between braces after specific selector. For example I have string like:
<div id="text-module-container-eb7272147" class="text-module-container"><style>#123-module-container-eb7272147 p{text-color:#211E22;bgcolor:test;} #text-module-container-eb7272147 p{color:#211E1E;} #text-module-container-eb7272147 p{color:#123444;} </style>
And now if I give selector #123-module-container-eb7272147 p
it should return text-color:#211E22;bgcolor:test;
I am able to get data between the braces but not with specific selector. This is tried code https://regex101.com/r/AESL8q/1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以对选择器和左大括号使用正向后查找,然后捕获不是右大括号的所有字符,并对右大括号使用正向前查找(可选):
/(?<=#123-module- container-eb7272147 p\{)[^}]+(?=\})/
(?<= )
完成的。[^}]+
来表示除右大括号之外的任何字符一次或多次。后面加一个问号会使其变得不贪心,但我认为没有必要。如果您使用点来匹配任何内容,就会出现这种情况。(?= )
完成的。您可以在这里测试:
或者在这里玩一下:https://regex101.com/r/N5cVKq/1
You can use a positive lookbehind with your selector and the opening brace then capture all chars which are not a closing brace and use a positive lookahead for the closing brace (optional):
/(?<=#123-module-container-eb7272147 p\{)[^}]+(?=\})/
(?<= )
.[^}]+
to say any char except the closing brace, once or more. Adding a question mark behind would make it ungreedy but I don't think it would be necessary. It would be the case if you use the dot to match anything.(?= )
.You can test it here:
Or play with it here: https://regex101.com/r/N5cVKq/1