我有这个
<p>
<strong>FITZGERALD</strong> (CONT’D)<br>
Shut up, boy, you don’t get no say in this.<br>
(back to Henry)<br>
And in case you hadn’t noticed, Captain, we’re seventeen men short of what we were.
(off the wounded trapper)<br>
Eighteen before long.
</p>
我要加类.wi-dialog
对于 p
并将每个文本括在括号中 (
)
喜欢<span class="wi-remark">(
)</span>
但先<br>
之后
所以我的段落看起来像这样
<p class="wi-dialog">
<strong>FITZGERALD</strong> (CONT’D)<br>
Shut up, boy, you don’t get no say in this.<br>
<span class="wi-remark">(back to Henry)</span><br>
And in case you hadn’t noticed, Captain, we’re seventeen men short of what we were.
<span class="wi-remark">(off the wounded trapper)</span><br>
Eighteen before long.
</p>
我无法解决第一行的问题。这是我的代码
$(document).ready(function(){
var dialog = $( "p:has(strong)" ).addClass( "wi-dialog" );
dialog.html(function(i, v) { return v.replace(/.*?(\(.*?\)).*?/g, '<span class="wi-remark">$&</span>') });
})
最佳答案
这是我的解决方案。
$(document).ready(function(){
var dialog = $( "p:has(strong)" ).addClass( "wi-dialog" );
dialog.html(function(i, v) { return v.replace(/(?<=\n)\s*(\(.*?\)).*?/g, '<span class="wi-remark">$&</span>') });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>
<strong>FITZGERALD</strong> (CONT’D)<br>
Shut up, boy, you don’t get no say in this.<br>
(back to Henry)<br>
And in case you hadn’t noticed, Captain, we’re seventeen men short of what we were.
(off the wounded trapper)<br>
Eighteen before long.
</p>
关于javascript - 使用 jQuery 在第一行后包装括号文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57023664/