Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

JavaScript to Replace All Occurrences of a Substring Within a String

You have a string with multiple occurrences of a substring within and you want to replace every occurrence of that substring with a different substring. Two built-in string methods make this operation extremely easy.

string.split(substring)

The .split method splits a string into pieces at a substring that you specify and returns the substrings as an array.

array.join(separator)

The .join method joins all elements of an array separating each element of the array with a substring that you specify, and returns a single string.

string.split(substring).join(replacement)

You can combine the call to the .split method and .join method to perform the operation in a single statement.

<html>
<head>

<script>
document.write("Original String:<br /><br />");
var strOriginal = "The blooming, bloody spider went up the spider web. The blooming, bloody rain came down and washed the spider out. The blooming, bloody sun came out and dried up all the rain. And the blooming, bloody spider came up the web again.";
document.write(strOriginal);

document.write("<br /><br />Modified String:<br /><br />");
var strModified = strOriginal.split("blooming, bloody").join("itsy bitsy");
document.write(strModified);

</script>

</head>
<body>

</body>
</html>

The example code above takes the string "The blooming, bloody spider went up the spider web. The blooming, bloody rain came down and washed the spider out. The blooming, bloody sun came out and dried up all the rain. And the blooming, bloody spider came up the web again." and replaces the substrings "blooming, bloody" within it with "itsy bitsy".

More Java Script Code:
• Disable IE8 Accelerators on Your Website
• Let Your Web site Visitors Set Font Size
• Easy Code to Add Mouse Trails to Your Webpage
• HTML5 Canvas Drag-and-Drop
• Where Did the User Click?
• Easy Java Script Form Validation
• JavaScript Code to Restrict Keyboard Entries
• Easy Java Script Windows
• Introduction to HTML5 Canvas
• Easy Picture Zoom Code

RSS Feed RSS Feed

Follow Stephen Bucaro Follow @Stephen Bucaro



Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2017 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268