Skip to content Skip to sidebar Skip to footer

Script Or CSS To Bold All Words That Are All CAPS

I'm a bit of a n00b but am trying to figure out a quick way to go through my html and put a tag on words that are in all caps. Can this be done through CSS or will i

Solution 1:

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;

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

Docs for replace.

Example


Post a Comment for "Script Or CSS To Bold All Words That Are All CAPS"