开发者

Removing the word 'Share' within some generated content

开发者 https://www.devze.com 2023-02-28 15:16 出处:网络
I need to rem开发者_C百科ove the word \'Share\' that is generated from a div (and replace it with nothing) using jQuery or JS. For example

I need to rem开发者_C百科ove the word 'Share' that is generated from a div (and replace it with nothing) using jQuery or JS. For example

Share It isn’t confirmed yet, but VatorNews is reporting that Google as tipped over the last couple of weeks has [...]

Can someone please help?


Steps:

  1. Get the value of the $(#div_id), using text() and then store it in a variable.
  2. Use regular expressions and string functions to remove it from the text, if present there.
  3. Using jquery or javascript, again use the text( textString ) function to set the value again, if you want to or use it as you wish.

For info on text(), refer here


Try with this:

$("#divId").text($("#divId").text().replace("Share", ""));


I have put the text into a variable called txt. How do I now search for the string 'Share'?

You can;

txt = txt.replace(/\bshare\b/ig, "");

\b to force a match on word boundary, ig for Ignore case, Global (replace all)


Thanks to everyone who helped with this. Much appreciated. To clarify what I needed: I had 4 divs with unique generated content. The string 'Share' always appeared at the beginning of a span tag within the content. I needed to remove that string. This is the code that solved it:

//Removes 'Share' string from featured posts excerpts on home page
jQuery('span.excerpt').each(function() {
    var share = jQuery(this);
    share.text(share.text().substr(6) );
});
0

精彩评论

暂无评论...
验证码 换一张
取 消