简体   繁体   中英

Pattern matching for strings in css

Do we have anything in css where styling could be applied to a matching set of keywords?

For example, I have a list of words {"history","leaders","emotions","time"...}

I would like to apply css style to all the keywords matching to above list in the below paragraph.


“Throughout human history , or greatest leaders and thinkers have used the power of words to transform our emotions , to enlist us in their causes, and to shape the course of destiny . Words can not only create emotions , they create actions. And from our actions flow the results of our lives.” Tony Robbins

Thanks all for inputs. I will go for javascript to edit the html code before applying css.

Here is one approach, showing how you can cycle through the array:

['history', 'leaders', 'emotions', 'time']

and bold each of the words in the text.

 var wordsToHighlight = ['history', 'leaders', 'emotions', 'time']; var paragraph2 = document.getElementsByTagName('p')[1]; var paragraph2Text = paragraph2.innerHTML; var paragraph2RichText = paragraph2Text; for (var i = 0; i < wordsToHighlight.length; i++) { var regex = new RegExp('('+ wordsToHighlight[i] + ')', 'gi'); paragraph2RichText = paragraph2RichText.replace(regex, '<strong>$1</strong>'); } paragraph2.innerHTML = paragraph2RichText; 
 <p>“Throughout human history, or greatest leaders and thinkers have used the power of words to transform our emotions, to enlist us in their causes, and to shape the course of destiny. Words can not only create emotions, they create actions. And from our actions flow the results of our lives.” Tony Robbins</p> <p>“Throughout human history, or greatest leaders and thinkers have used the power of words to transform our emotions, to enlist us in their causes, and to shape the course of destiny. Words can not only create emotions, they create actions. And from our actions flow the results of our lives.” Tony Robbins</p> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM