JavaScript Replace Text

Find and replace Text using JavaScript

JavaScript can be used to replace text dynamically. JavaScript can eliminate the reloading of a page to replace text on the server side. JavaScript DOM objects can be utilized to find and replace text.

Specific identifiers will be used to speedup the process of finding and replacing text. Tips from the regular expressions article will be used to find specific text.

This tutorial uses JavaScript with HTML. The text to replace and the the replacement text are both declared in JavaScript variables.

    Tools are required:

  • Text editor.
  • HTML.
  • JavaScript.
  • Browser to view output.

Optional Download and install suitable text editor

Any text editor with regular expression support will work. To select a reviewed lightweight programming editor read the Lightweight Programming Editors.

JavaScript-ReplaceText.html file

  Copyright 2011 Edward <>
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  GNU General Public License for more details.
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  MA 02110-1301, USA.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="" xml:lang="en" lang="en">

	<title> JavaScript Replace Text</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<script type="text/JavaScript">
		function replaceText() {
			// Get the full content to be replaced
			originalContent = document.getElementById("original-content").innerHTML;

			// Specific match
			var specificMatch = "HERE";
			// Match is case sensitive and every occurrence (global = g)
			var matchText = new RegExp(specificMatch, "g");     
			// Replacement text
			var replacementText = "THERE";
			// Replace every occurrence of the match
			replacedContent = originalContent.replace(matchText, replacementText);

			// Overwrite the original content with the replaced content
			  document.getElementById("original-content").innerHTML = replacedContent;

	<h2>'s JavaScript demonstration of text replacement</h2>
	<p id="original-content">The text to replace is HERE</p>
	<a href="#" onClick="replaceText();">Replace HERE with THERE</a>


The original content has an id identified by “original-content” which is used to assign the variable “originalContent”. The specific match text and replacement text are “HERE” and “THERE” respectively. The match is global which means every occurrence. After performing a match, the new text replaces the old text.

How to Use:

Run the JavaScript-ReplaceText.html file in your favourite browser.
Click the “Replace HERE with THERE” link.

Demonstration:’s JavaScript demonstration of text replacement

The text to replace is HERE

Replace HERE with THERE


JavaScript was used to find and replace text dynamically. JavaScript eliminates the need of having to run server side code. Regular expressions and specific DOM identifiers are used to speed up the JavaScript find and replace process.


  1. Do not rely on JavaScript only, have a fall-back to server-side method.
  2. A “noscript” warning is appropriate, see No Script Tag Article.
  3. Reuse as much JavaScript as possible to keep optimal loading times.

Tags: , , , , ,

This entry was posted on Wednesday, October 5th, 2011 at 12:00 am and is filed under Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “JavaScript Replace Text”

  1. Ojambo JavaScript Replace Text Video 0034 Says:

    October 21st, 2011 at 12:08 am

    […] for the JavaScript replace text article. Part One of […]

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>