當我對標題和說明使用正則運算式函式時它會起作用,但是當我添加更多匹配的標簽時它不起作用
這是下面的代碼,它可以正常作業,沒有任何問題
$(".sidebar").each(function() {
var e = $(this),
t = e.find(".widget").text();
if (t) {
var a = t.match(/title=\(([^)]*)\)\s caption=\(([^)]*)\)/);
alert(t);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='sidebar'>
<div class='widget'>title=(example1) caption=(example2)</div>
</div>
但問題是當我嘗試使用多個匹配的標簽時,它不會像
<div class='widget'>title=(example1) caption=(example2) button=(example3) price=(example4) off=(example5)</div>
我努力為這個值使用與上面相同的正則運算式,但它沒有用
我還想添加這些示例文本,輸出看起來像example1 example2 example3 example4 example5但我不知道用 regex 函式完成
非常感謝任何幫助或建議
uj5u.com熱心網友回復:
而不是使用match(),考慮使用exec(),這也將允許您訪問捕獲組。理想情況下,您的捕獲組應該捕獲=字符后面括號內的值,因此您可以這樣撰寫正則運算式:
var re = /\w ?=\((.*?)\)/gi;
然后,只需運行while回圈以遞回呼叫該exec()方法,直到找不到更多匹配項:
$(".sidebar").each(function() {
var e = $(this);
var t = e.find(".widget").text();
if (t) {
var result;
var results = [];
var re = /\w ?=\((.*?)\)/gi;
while ((result = re.exec(t)) !== null) {
console.log(result[1]);
results.push(result[1]);
}
console.log('Logging all captured values:\n', results);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='sidebar'>
<div class='widget'>title=(example1) caption=(example2) button=(example3) price=(example4) off=(example5)</div>
</div>
Even better: you don't even need jQuery for this:
document.querySelectorAll('.sidebar .widget').forEach(el => {
const t = el.innerText.trim();
if (t) {
let result;
const results = [];
const re = /\w ?=\((.*?)\)/gi;
while ((result = re.exec(t)) !== null) {
console.log(result[1]);
results.push(result[1]);
}
console.log('Logging all captured values:\n', results);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='sidebar'>
<div class='widget'>title=(example1) caption=(example2) button=(example3) price=(example4) off=(example5)</div>
</div>
uj5u.com熱心網友回復:
Here's an example on how to get any value given this format:
Match anything that is not a
)one or more times.
Must be preceded by\w =(
and must be followed by)
Better explained in This Regex101 Example
$(".sidebar").find(".widget").each(function() {
const txt = $.trim($(this).text()); // Get trimmed text
if (!txt) return; // Do nothing
const matches = txt.match(/(?<=\b\w =\()[^)] (?=\))/g)||[];
matches.forEach(m => {
console.log(m);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='sidebar'>
<div class='widget'>title=(example1) caption=(example2) button=(example3) price=(example4) off=(example5)</div>
<div class="widget">ola</div>
<div class="widget"></div>
<div class="widget">The magic happens=(here) :)</div>
</div>
uj5u.com熱心網友回復:
In your code, you use alert(t); which is the result from t = e.find(".widget").text();
If you use the result from var a = t.match(/title=\(([^)]*)\)\s caption=\(([^)]*)\)/); then the regex will return a match for title= and caption= which either have a value in a capture group, but the match should be in that order.
You will get a single match as there is no /g flag, and there is also only 1 occurrence in the example data.
What you could do is make your expression a bit less restrictive, and instead of matching specifically on title and capture, match 1 or more non whitespace characters other than an equals sign instead.
Use the /g flag to get all the matches instead of a single match.
[^\s=] =\(([^)]*)\)
[^\s=]Match 1 times any char other than=or a whitespace char=Match literally\(Match(([^)]*)Capture in group 1 optional characters other than)or useinstead of*for 1 or more\)Match)
A regex demo to see the captured group values:
$(".sidebar").each(function() {
var e = $(this),
t = e.find(".widget").text();
if (t) {
var a = t.matchAll(/[^\s=] =\(([^)]*)\)/g);
console.log(Array.from(a, m => m[1]));
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='sidebar'>
<div class='widget'>title=(example1) caption=(example2) button=(example3) price=(example4) off=(example5)</div>
</div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/310065.html
標籤:javascript 查询 正则表达式 功能 比赛
下一篇:用固定值計算輸入值
