0

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

0

1 Answer 1

3

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, '');
Sign up to request clarification or add additional context in comments.

2 Comments

@Caspar: jQuery is also javascript at its core. The replace function is what you need to use there :)
I agree completely with your answer, replace however is not a jQuery function. Just as a sidenote: the answer has just partially something to do with jQuery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.