将添加到中的每个单词
我想在中的每个单词中添加。 该链接应包含它已被包裹的单词。 该网页有许多不同的h3。
https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/add https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/links
https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/to https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/each https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/word
结果应如下所示:
https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/add https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/links
https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/to https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/each https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/word
我不知道是否可能(或好)使用PHP其他jQuery / JS会很好!
-
您可以使用
html()
函数的html()
来更新值 -
要使用锚标记更新它,请使用带有
regex
replace()
$('h3').html(function(i, v) { return v.replace(/(\s*)(\w+)(\s*)/g, '$1https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/$2$3'); });
add links
to each word
给定答案的代码不适用于嵌入式标签和Unicode字。
这是一个更好的万无一失的替代品:
$('h3').contents().filter(function() { return this.nodeType === 3; }).replaceWith(function() { return this.nodeValue.replace(/(\S+)(?=\s+|$)/g, 'https://stackoverflow.com/questions/31707823/add-a-to-every-word-inside-h3/$&'); });
add links to each word
with Юникод support & ignoring this