Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript modify only certain words in a pargraph

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #251799
    triplebit
    Participant

    Here is a snippet that I found in the internet that adds a class to specific substring of a paragraph.
    Problem is that when the paragraph contains elements that are not words – like an element of type , it eliminates this element completely from the outcome.
    Can someone modify it to preserve the element as well?
    Thx

    jQuery(document).ready( function ($) {

    var oldString = ‘some words’,
    newString = ‘some words‘,
    newText = $( “p:contains(‘some words’)” ).text().replace(RegExp(oldString,”gi”),newString);
    $( “p:contains(‘some words’)” ).html(newText);

    }); // jQuery(document).ready( function ($) {

    #251820
    Shikkediel
    Participant
    newText = $('p:contains("some words")' ).html().replace(RegExp(oldString, 'gi'), newString);
    

    codepen.io/anon/pen/xqKomm

    Including such a small demo to a question would be appreciated.

Viewing 2 posts - 1 through 2 (of 2 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.