
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript String - replace() Method
Description
This method finds a match between a regular expression and a string, and replaces the matched substring with a new substring.
The replacement string can include the following special replacement patterns −
Pattern | Inserts |
---|---|
$$ | Inserts a "$". |
$& | Inserts the matched substring. |
$` | Inserts the portion of the string that precedes the matched substring. |
$' | Inserts the portion of the string that follows the matched substring. |
$n or $nn | Where n or nn are decimal digits, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object. |
Syntax
The syntax to use the replace() method is as follows −
string.replace(regexp/substr, newSubStr/function[, flags]);
Argument Details
regexp − A RegExp object. The match is replaced by the return value of parameter #2.
substr − A String that is to be replaced by newSubStr.
newSubStr − The String that replaces the substring received from parameter #1.
function − A function to be invoked to create the new substring.
flags − A String containing any combination of the RegExp flags: g - global match, i - ignore case, m - match over multiple lines. This parameter is only used if the first parameter is a string.
Return Value
It simply returns a new changed string.
Example
Try the following example.
<html> <head> <title>JavaScript String replace() Method</title> </head> <body> <script type = "text/javascript"> var re = /apples/gi; var str = "Apples are round, and apples are juicy."; var newstr = str.replace(re, "oranges"); document.write(newstr ); </script> </body> </html>
Output
oranges are round, and oranges are juicy.
Example
Try the following example; it shows how to switch words in a string.
<html> <head> <title>JavaScript String replace() Method</title> </head> <body> <script type = "text/javascript"> var re = /(\w+)\s(\w+)/; var str = "zara ali"; var newstr = str.replace(re, "$2, $1"); document.write(newstr); </script> </body> </html>
Output
ali, zara