亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

使用正則表達(dá)式匹配id值
P粉757556355
P粉757556355 2023-08-30 16:13:18
0
1
686
<p>我想找到所有值為<code>id</code>屬性由以下組成的<code>g</code>節(jié)點(diǎn)的子節(jié)點(diǎn):</p> <pre class="brush:php;toolbar:false;">a[number]-[一個(gè)或多個(gè)字符] // 例子: // - id="a1-a" // - id="a1-b" // - id="a1-abcd" // - id="a10-f" // - id="a0-z" // - id="b1-a" // 不合法 // - id="a1-2" // 不合法</pre> <p>所以我嘗試了:</p> <pre class="lang-js prettyprint-override"><code>const items = gElement.querySelectorAll(`[id^='a[0-9]+-[a-zA-Z]+']`) </code></pre> <p>但是,它不起作用。</p>
P粉757556355
P粉757556355

全部回復(fù)(1)
P粉237647645

在你的查詢選擇器中,你使用的模式 ([0-9]+) 沒有被解釋為正則表達(dá)式。使用 RegExp 構(gòu)造函數(shù)從字符串創(chuàng)建一個(gè)正則表達(dá)式對(duì)象:

const regex = new RegExp('^a[0-9]+-[a-zA-Z]+$');
const parentElement = document.querySelector('#parent-element');
const items = parentElement.querySelectorAll(`[id]`);
const children = Array.from(items).filter(item => regex.test(item.id));

console.log(children); 
<div id="parent-element">
  <p id="a1-a">Child 1</p>
  <p id="a1-b">Child 2</p>
  <p id="INVALID-1">Child 3</p>
  <p id="a10-f">Child 4</p>
  <p id="INVALID-2">Child 5</p>
  <p id="b1-a">Child 6</p>
  <p id="a1-2">Child 7</p>
</div>
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板