提问人:Rikesh 提问时间:3/8/2022 更新时间:3/8/2022 访问量:44
正则表达式捕获特定单词后面的大括号
Regex to capture braces after specific word
问:
我想在特定选择器之后捕获大括号之间的字符串。例如,我有这样的字符串:
<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
答:
1赞
Patrick Janser
3/8/2022
#1
您可以对选择器和左大括号使用正向后视,然后捕获所有不是右大括号的字符,并对右大括号使用正前瞻(可选):
/(?<=#123-module-container-eb7272147 p\{)[^}]+(?=\})/
- 积极的回顾是用 完成的。
(?<= )
- 对于选择器,您必须转义一些字符,通常如果您有类选择器,则应转义点。开场大括号也。
- 您想要的大括号之间的匹配是说出除右大括号以外的任何字符,一次或多次。在后面添加一个问号会使它变得不干净,但我认为没有必要。如果您使用点来匹配任何内容,情况就是如此。
[^}]+
- 积极的展望是用 完成的。
(?= )
您可以在此处进行测试:
/**
* Escape characters which have a meaning in a regular expression.
*
* @param string The string you need to escape.
* @returns The escaped string.
*/
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
let button = document.querySelector('#extract');
button.addEventListener('click', function(event) {
let html = document.querySelector('#html').value;
let selector = document.querySelector('#selector').value;
let pattern = new RegExp('(?<=' + escapeRegExp(selector) + '\s*\{)[^}]+(?=\})');
let matches = pattern.exec(html);
if (matches) {
alert("The extracted CSS rules:\n\n" + matches[0]);
}
event.preventDefault();
});
html, body {
font-family: Arial, sans serif;
font-size: 14px;
}
fieldset {
min-width: 30em;
padding: 0;
margin: 1em 0;
border: none;
display: flex;
}
label {
margin-right: 1em;
width: 6em;
}
input[type="text"],
textarea {
width: calc(100% - 7em);
min-width: 20em;
margin: 0;
padding: .25em .5em;
}
input[type="submit"] {
margin-left: 7.1em;
padding: .2em 1em;
}
<form action="#">
<fieldset>
<label for="selector">Selector: </label>
<input type="text" id="selector" name="selector"
value="#123-module-container-eb7272147 p">
</fieldset>
<fieldset>
<label for="">HTML code:</label>
<textarea id="html" name="html" cols="30" rows="10"><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><div style="background-color: rgb(168, 27, 219); color: rgb(33, 30, 30);"><span style="color:#3498db;">Click the edit button to replace this conte</span>nt with your own.</div></div></textarea>
</fieldset>
<fieldset>
<input type="submit" id="extract" value="Extract the CSS rules">
</fieldset>
</form>
或者在这里玩它:https://regex101.com/r/N5cVKq/1
评论
0赞
Rikesh
3/8/2022
谢谢帕特里克,这就像魅力一样。有什么方法可以动态替换图案中的零件?#123-module-container-eb7272147 p
0赞
Patrick Janser
3/8/2022
您可以使用构建模式并快速创建函数来转义选择器。在此处查看解决方案:stackoverflow.com/a/6969486/653182let pattern = new RegExp(...)
下一个:用于在文本中查找链接的正则表达式
评论
/#123-module-container-eb7272147\s+p{([^}]+)}/i
p
#123-module-container-eb7272147 p{(.+?)}
[^]]+
.+?