我不太擅长正则表达式,我也没有完全理解这个,我从中得到的只是它找到每个 h1 并在它的最后一个词中添加一个类。
$("h1").html(function(index, old) {
return old.replace(/(\b\w+)$/, '<span class="myClass">$1</span>');
});
我试图通过最后两个字符让它工作
最佳答案
这里是解释:
/ : regex delimter
( : begin capture group #1
\b : word boundary
\w+ : one or more word character (same as [a-zA-Z0-9_]+)
) : end of group
$ : end of string
/ : regex delimiter
它匹配字符串的最后一个词,即h1标签的最后一个词。
关于javascript - 这个正则表达式的解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27250706/