正则表达式捕获特定单词后面的大括号

Regex to capture braces after specific word

提问人:Rikesh 提问时间:3/8/2022 更新时间:3/8/2022 访问量:44

问:

我想在特定选择器之后捕获大括号之间的字符串。例如,我有这样的字符串:

<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 ptext-color:#211E22;bgcolor:test;

我能够在大括号之间获取数据,但不能使用特定的选择器。这是尝试过的代码 https://regex101.com/r/AESL8q/1

JavaScript PHP jQuery 正则表达式 匹配

评论

1赞 anubhava 3/8/2022
尝试/#123-module-container-eb7272147\s+p{([^}]+)}/i
0赞 freedomn-m 3/8/2022
"我给选择器 #123-module-container-eb7272147 p it“ - ”“存在?正如标记的jquery一样,如果您将其作为jquery的选择器,则不会返回任何元素(给定提供的字符串),因为没有元素。p
0赞 freedomn-m 3/8/2022
稍微简化的正则表达式,但您需要使用组,而不是匹配项:regex101.com/r/obIoHN/1#123-module-container-eb7272147 p{(.+?)}
0赞 anubhava 3/8/2022
@freedomn-m:这几乎与我在评论中发布的正则表达式相同。只是这比 .[^]]+.+?
0赞 freedomn-m 3/8/2022
@anubhava对不起,对效率一无所知(除非 10mill+ 搜索,否则不太可能重要),只是稍微短一点,并且带有正则表达式101 链接

答:

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">&lt;div id=&quot;text-module-container-eb7272147&quot; class=&quot;text-module-container&quot;&gt;&lt;style&gt;#123-module-container-eb7272147 p{text-color:#211E22;bgcolor:test;} #text-module-container-eb7272147 p{color:#211E1E;} #text-module-container-eb7272147 p{color:#123444;} &lt;/style&gt;&lt;div style=&quot;background-color: rgb(168, 27, 219); color: rgb(33, 30, 30);&quot;&gt;&lt;span style=&quot;color:#3498db;&quot;&gt;Click the edit button to replace this conte&lt;/span&gt;nt with your own.&lt;/div&gt;&lt;/div&gt;</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(...)