javascript - jQuery - 使用正则表达式使链接中的模式变为粗体
问题描述
我有一个日期列表,我尝试使用 jQuery 和正则表达式将链接中的每个日期设为粗体。这是我到目前为止得到的:
$date = '/([0-2][0][0-9][0-9]).([0-1][0-9]).([0-3][0-9])/gi';
$('a:contains('+$date+')').css('font-weight','600');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<a href="#">2018.03.08 Event #1</a>
</li>
<li>
<a href="#">2018.02.12 Event #2</a>
</li>
<li>
<a href="#">2017.12.04 Event #3</a>
</li>
</ul>
我的代码有什么问题?有一个更好的方法吗?
我是 JS/jQuery 的菜鸟,非常感谢您的帮助!
解决方案
首先,解决问题:
// a date-pattern regular expression literal:
let datePattern = /([0-2][0-9]{3,3}\.[0-1][0-9]\.[0-3][0-9])/gi;
// select all <a> elements,
// and iterate over the returned collection with the
// .html() method:
$('a').html(function() {
// here we return the modified string of text,
// wrapping any patterns matching the date pattern,
// with the '<b>' and '</b>' tags; as this text is
// returned to the HTML method this is parsed as
// HTML:
return this.textContent.replace(datePattern, '<b>$1</b>');
});
let datePattern = /([0-2][0-9]{3,3}\.[0-1][0-9]\.[0-3][0-9])/gi;
$('a').html(function() {
return this.textContent.replace(datePattern, '<b>$1</b>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<a href="#">2018.03.08 Event #1</a>
</li>
<li>
<a href="#">2018.02.12 Event #2</a>
</li>
<li>
<a href="#">2017.12.04 Event #3</a>
</li>
</ul>
还值得注意的是,虽然 jQuery 可以——而且确实——让一些事情变得更容易,但上面的内容对于纯 JavaScript 来说很简单:
let datePattern = /([0-2][0-9]{3,3}\.[0-1][0-9]\.[0-3][0-9])/gi;
// converting the result of the contained expression
// into an Array, in order to gain access to Array methods:
Array.from(
// retrieving all <a> elements from the document:
document.querySelectorAll('a')
// using Array.prototype.forEach() to iterate over
// the Array of <a> elements:
).forEach(function(aElement) {
// 'aElement' refers to the current <a> element
// of the Array of <a> elements.
// here we update the innerHTML of the current <a> element
// to be equal to the text of the current <a> element, after
// wrapping any date-patterns - as before - with the '<b>'
// and '</b>' tags:
aElement.innerHTML = aElement.textContent.replace(datePattern, '<b>$1</b>');
});
let datePattern = /([0-2][0-9]{3,3}\.[0-1][0-9]\.[0-3][0-9])/gi;
Array.from(
document.querySelectorAll('a')
).forEach(function(aElement) {
aElement.innerHTML = aElement.textContent.replace(datePattern, '<b>$1</b>');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li>
<a href="#">2018.03.08 Event #1</a>
</li>
<li>
<a href="#">2018.02.12 Event #2</a>
</li>
<li>
<a href="#">2017.12.04 Event #3</a>
</li>
</ul>
现在解决问题:
$date = '/([0-2][0][0-9][0-9]).([0-1][0-9]).([0-3][0-9])/gi';
在这里,您声明了一个全局变量(不好的做法,因为该全局变量可以在其他地方不可预测地使用/覆盖)。此外,正则表达式被包裹在一个字符串文字中,这意味着正则表达式是一个字符串,而不是一个正则表达式。
此外:contains()
,正如文档中明确指出的那样,选择器仅适用于字符串。
<a>
因此,在您的原始代码中,您有一个字符串(看起来像一个正则表达式),它正在任何元素中被搜索,但从未找到。
此外,如果发现您的css()
方法调用的最终结果是将<a>
元素本身包装在一个<b>
元素中,而不是包装日期字符串。我认为这是你的计划。
参考:
- JavaScript:
- jQuery:
推荐阅读
- javascript - 如何使用我在 HTML 上创建的下载按钮下载 pdf 简历
- input - 我正在尝试制作一个使用谷歌表上的条形码扫描仪自动更新的库存表
- python - 如何在python中水平排列“字符串列”?
- ibm-watson - 是否可以获得与给定 workspace_id 关联的 Watson 代理版本?
- c# - 使用 goto 时如何解决此编译错误“使用未分配的局部变量 'hak'”?
- python - 在 GCP cloudbuild 中安装 python 要求的问题
- android - 有什么方法可以从库的源代码中查看示例?
- javascript - 带有 Sequelize 6.3 的 PostgreSQL 12.2 - UPSERT 一组数据并希望 ON CONFLIT DO UPDATE 合并当前现有的 JSONB 和排除的 JSONB
- python - 为什么这个 python 代码可以在我的 XUbuntu (Ubuntu 20.04) 机器上运行,但不能在我的 Ubuntu 18.04 服务器上运行
- jmeter - Jmeter:活动浏览器选项卡