5

Find a specific word in paragraph

 3 years ago
source link: https://www.codesd.com/item/find-a-specific-word-in-paragraph.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Find a specific word in paragraph

advertisements

I am finding a specific word from a paragraph and want to make it bold. I have done below code but it's not working.

HTML

<div>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</div>

Script

var text = /Lorem/ig;
    $('div')
    .filter(function(){
        return text.test( $(this).text() )
    }).wrap('<strong></strong>');

Fiddle Demo


There is lil' plugin created by elclanrs

$.fn.wrapInTag = function(opts) {

   var tag = opts.tag || 'strong'
, words = opts.words || []
, regex = RegExp(words.join('|'), 'gi') // case insensitive
, replacement = '<'+ tag +'>Lorem</'+ tag +'>';

 return this.html(function() {
   return $(this).text().replace(regex, replacement);
  });};

// Usage
$('div').wrapInTag({
  tag: 'strong',
  words: ['Lorem']   //can be comma seprated like  ['Lorem','ipsum']
});

Working Demo


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK