回答編集履歴
2
調整
test
CHANGED
@@ -34,3 +34,10 @@
|
|
34
34
|
<tr></tr>
|
35
35
|
</table>
|
36
36
|
```
|
37
|
+
最近のjsの正規表現ならこんな感じでいけます
|
38
|
+
```javascript
|
39
|
+
const html="<td>jQuery</td>";
|
40
|
+
const reg=/(?<=<td(| .?)>).+?(?=<\/td>)/g;
|
41
|
+
const result=html.replace(reg,`<a href="test">$&</a>`);
|
42
|
+
console.log(result);
|
43
|
+
```
|
1
ちょうせい
test
CHANGED
@@ -17,4 +17,20 @@
|
|
17
17
|
このHTMLを正規表現でつかもうとする処理も効率的なやり方ではないですね
|
18
18
|
また正規表現としても中途半端で今となってはあまりおすすめできないサンプルだと思います。
|
19
19
|
|
20
|
-
|
20
|
+
素のjsに書き下すとこんなかんじ
|
21
|
+
```javascript
|
22
|
+
<script>
|
23
|
+
window.addEventListener('DOMContentLoaded', ()=>{
|
24
|
+
document.querySelector('tr:first-child').appendChild(Object.assign(document.createElement('td'),{textContent:'jQuery',data:{link:'http//jquery.com/'}}));
|
25
|
+
document.querySelector('tr:last-child').appendChild(Object.assign(document.createElement('td'),{textContent:'jQuery UI',data:{link:'http://jqueryui.com'}}));
|
26
|
+
document.querySelectorAll('tr').forEach((ele,index)=>{
|
27
|
+
ele.insertAdjacentHTML('beforeend',ele.innerHTML.replace(/(<td>)([^<]+)(<\/td>)/,`$1<a href="${ele.querySelector('td').data.link}">$2</a>$3`));
|
28
|
+
ele.insertAdjacentHTML('afterbegin',`<td>${index+1}</td>`);
|
29
|
+
});
|
30
|
+
});
|
31
|
+
</script>
|
32
|
+
<table>
|
33
|
+
<tr></tr>
|
34
|
+
<tr></tr>
|
35
|
+
</table>
|
36
|
+
```
|