脚本或CSS来粗体显示所有CAPS的单词 [英] Script or CSS to bold all words that are all CAPS

查看:78
本文介绍了脚本或CSS来粗体显示所有CAPS的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个n00b,但我试图找出一个快速的方法来通过我的html,并把一个< strong> 标签放在单词上全部大写。这可以通过CSS来完成,还是必须是JavaScript?无论哪种方式,该方法是什么?感谢!

I'm a bit of a n00b but am trying to figure out a quick way to go through my html and put a <strong> tag on words that are in all caps. Can this be done through CSS or will it have to be JavaScript? Either way, what would be the method? Thanks!

推荐答案

获取元素的内容(在我的例子中是一个普通的div),并用正则表达式。

Get the Content of your element (in my example a plain div), and search with a regex for capsed words.

var div = document.querySelector("div");
var html = div.innerHTML;
html = html.replace(/(\b[A-Z]{2,}\b)/g,"<strong>$1</strong>");
div.innerHTML = html;

关键是字符类 [AZ] 它只接受以单词边界包围的大写字母 \ b

The keypoint is the characterclass [A-Z] which only accepts capital letters enclosed in word borders \b.

Docs for 替换

示例

这篇关于脚本或CSS来粗体显示所有CAPS的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆