我的 JS 中有以下函数
var p = $('.page p');
p.html(function(index, oldHtml) {
return oldHtml.replace(/\b(\w+?)\b/g, '<span class="word">$1</span>')
});
这会分解段落中的每个单词。然而,带有撇号的单词(例如 don't、craig's)会被分成两部分(例如 don 和 t)
如何修改代码以使这些单词完整并保留在撇号中?
谢谢
最佳答案
你尝试过吗:
var p = $('.page p');
p.html(function(index, oldHtml) {
console.log(oldHtml);
return oldHtml.replace(/([\w']+)/g, '<span class="word">$1</span>');
});
.word {
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="page">
<p>I'm so happy that I can't stop jumping up and down.</p>
</div>
关于javascript - 在文本中保留撇号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28896576/