用于在 (' 和 ' 之间进行选择的正则表达式
Regex for selecting beween (' and '
我试图寻找答案,但没有适合我需要的答案...
这是我的代码:
<div><svg onclick="addIngredient('Bacon', -1);"><path></path></svg>
<button onclick="addIngredient('Bacon', 1);"></button><p>6 Bacon</p></div>
<div><svg onclick="addIngredient('Paprika', -1);"><path></path></svg>
<button onclick="addIngredient('Paprika', 1);"></button><p>3 Paprika</p></div>
<div><svg onclick="addIngredient('Sliced Meat', -1);"><path></path></svg>
<button onclick="addIngredient('Sliced Meat', 1);"></button><p>1 Sliced Meat</p></div>
我想捕捉之间的话语
svg onclick="addIngredient('
而 '
词后。例如,我想检索单词 Bacon
、Paprika
或 Sliced Meat
我试过这样做,但没用..
var stringy = '<div><svg onclick="addIngredient('Bacon', -1);"><path></path></svg>
<button onclick="addIngredient('Bacon', 1);"></button><p>6 Bacon</p></div>
<div><svg onclick="addIngredient('Paprika', -1);"><path></path></svg>
<button onclick="addIngredient('Paprika', 1);"></button><p>3 Paprika</p></div>
<div><svg onclick="addIngredient('Sliced Meat', -1);"><path></path></svg>
<button onclick="addIngredient('Sliced Meat', 1);"></button><p>1 Sliced Meat</p></div>';
var result = stringy.match("svg onclick="addIngredient(([^}]*)')");
console.log(result);
我怎样才能做对?
您可以使用以下正则表达式。它使用 .*?
匹配尽可能少的字符,直到出现下一个 '
<svg.+?onclick="addIngredient\('(.*?)'
这是一个 运行 示例。您需要使用 .exec()
函数来仅获取每次出现的 </code> 组。</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>var stringy = document.getElementById("main").innerHTML; // read the HTML instead of hard-coding it here as a string
var regex = /<svg.+?onclick="addIngredient\('(.*?)'/g;
var match = regex.exec(stringy);
while(match !== null) {
console.log(match[1]);
match = regex.exec(stringy);
}
<div id="main">
<div><svg onclick="addIngredient('Bacon', -1);"><path></path></svg>
<button onclick="addIngredient('Bacon', 1);"></button><p>6 Bacon</p></div>
<div><svg onclick="addIngredient('Paprika', -1);"><path></path></svg>
<button onclick="addIngredient('Paprika', 1);"></button><p>3 Paprika</p></div>
<div><svg onclick="addIngredient('Sliced Meat', -1);"><path></path></svg>
<button onclick="addIngredient('Sliced Meat', 1);"></button><p>1 Sliced Meat</p></div>
</div>
我试图寻找答案,但没有适合我需要的答案...
这是我的代码:
<div><svg onclick="addIngredient('Bacon', -1);"><path></path></svg>
<button onclick="addIngredient('Bacon', 1);"></button><p>6 Bacon</p></div>
<div><svg onclick="addIngredient('Paprika', -1);"><path></path></svg>
<button onclick="addIngredient('Paprika', 1);"></button><p>3 Paprika</p></div>
<div><svg onclick="addIngredient('Sliced Meat', -1);"><path></path></svg>
<button onclick="addIngredient('Sliced Meat', 1);"></button><p>1 Sliced Meat</p></div>
我想捕捉之间的话语
svg onclick="addIngredient('
而 '
词后。例如,我想检索单词 Bacon
、Paprika
或 Sliced Meat
我试过这样做,但没用..
var stringy = '<div><svg onclick="addIngredient('Bacon', -1);"><path></path></svg>
<button onclick="addIngredient('Bacon', 1);"></button><p>6 Bacon</p></div>
<div><svg onclick="addIngredient('Paprika', -1);"><path></path></svg>
<button onclick="addIngredient('Paprika', 1);"></button><p>3 Paprika</p></div>
<div><svg onclick="addIngredient('Sliced Meat', -1);"><path></path></svg>
<button onclick="addIngredient('Sliced Meat', 1);"></button><p>1 Sliced Meat</p></div>';
var result = stringy.match("svg onclick="addIngredient(([^}]*)')");
console.log(result);
我怎样才能做对?
您可以使用以下正则表达式。它使用 .*?
匹配尽可能少的字符,直到出现下一个 '
<svg.+?onclick="addIngredient\('(.*?)'
这是一个 运行 示例。您需要使用 .exec()
函数来仅获取每次出现的 </code> 组。</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre><code>var stringy = document.getElementById("main").innerHTML; // read the HTML instead of hard-coding it here as a string
var regex = /<svg.+?onclick="addIngredient\('(.*?)'/g;
var match = regex.exec(stringy);
while(match !== null) {
console.log(match[1]);
match = regex.exec(stringy);
}
<div id="main">
<div><svg onclick="addIngredient('Bacon', -1);"><path></path></svg>
<button onclick="addIngredient('Bacon', 1);"></button><p>6 Bacon</p></div>
<div><svg onclick="addIngredient('Paprika', -1);"><path></path></svg>
<button onclick="addIngredient('Paprika', 1);"></button><p>3 Paprika</p></div>
<div><svg onclick="addIngredient('Sliced Meat', -1);"><path></path></svg>
<button onclick="addIngredient('Sliced Meat', 1);"></button><p>1 Sliced Meat</p></div>
</div>