Richard77
Richard77

Reputation: 21671

how to manipulate strings in jQuery (especially, finding sub-string in a string)

let's say, for instance, I have this

<h2>I don't love Programming</h2>

I'd like to remove 'don't' from the text inside the h2 tag.

So far, I've done this

var content = $(h2).text();

That's allow me to get the text 'I don't love Programming'. But I need a way to locate don't and replace it by something else or simply remove it.

Thanks for helping

Upvotes: 0

Views: 2700

Answers (1)

Sarfraz
Sarfraz

Reputation: 382909

You can use the replace function:

var content = $(h2).text().replace("don't", "");

To come up with:

I love Programming

If you want to remove all instances of a string from given string, you can use regex with /g modifier:

var content = $(h2).text().replace(/don't/g, '');

Upvotes: 3

Related Questions