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

发布于 2025-01-12 21:48:22 字数 604 浏览 1 评论 0原文

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

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2025-01-19 21:48:22

您可以对选择器和左大括号使用正向后查找,然后捕获不是右大括号的所有字符,并对右大括号使用正向前查找(可选):

/(?<=#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, '\\
amp;');
}

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

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\{)[^}]+(?=\})/

  • The positive lookbehind is done with (?<= ).
  • For the selector, you'll have to escape some chars, typically if you have a class selector the dot should be escaped. The opening brace also.
  • The match you want between the braces is [^}]+ 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.
  • The positive lookahead is done with (?= ).

You can test it here:

/**
 * 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, '\\
amp;');
}

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>

Or play with it here: https://regex101.com/r/N5cVKq/1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文