Jquery replace() Vs replacewith()



Hello,

What is the difference between jquery replace() and Jquery replacewith()?

Mark



Jquery replaceWith is used to replace a text or HTML content with another one.

The syntax runs as follows:

.replaceWith( newContent )

Jquery replace allows us to "find" using regular expression and then replace with the new content.

Syntax for jquery replace() is:

.replace(<regular expression>, "newContent");

As an example to jquery replacewith():

<div class="container">
  <div class="inner first">Hello</div>
  <div class="inner second">And</div>
  <div class="inner third">Goodbye</div>
</div>

$('.second').replaceWith('<h2>New heading</h2>');

would give output:

<div class="container">
  <div class="inner first">Hello</div>
  <h2>New heading</h2>
  <div class="inner third">Goodbye</div>
</div>

For example on Jquery find and replace, click on the link.