The JavaScript replace function searches for a specified text inside an original string and updates the searched substring with the newly specified string.
By default, the JavaScript replace() function replaces the first occurrence of a substring in a main string. To replace all occurrences, we can use the built-in replaceAll() function or use a regular expression with the g flag.
JavaScript replace function syntax
The syntax of the replace() function to substitute a substring within a given string is as shown below.
String_Object.replace(old, new)
From the above syntax, you can see that the JavaScript replace() function accepts two parameter values.
- String_Object: It is the original string where the replacement happens. The replace() function performs a search operation on this string and updates a portion of it.
- old: It is the first argument, and it may be a substring or a regular expression. Whatever you place here, the function will change it with a new string.
- new: Substring you want to insert into the String_Object. It will replace the matching text (old) in a given string object with a new substring.
Return Value: The JavaScript replace() function does not modify the original string. Instead, it returns a new string with the replacements.
For advanced pattern matching, use the regular expression as the replace() function’s first argument.
str.replace(/pattern/, new)
We can also use the function as the second argument. If we pass a function as the replace() function’s second argument, the function will be invoked for every match, and the function’s return value is used as the replacement text (new).
str.replace(old, function)
The complex version of the replace() function is
str.replace(old/pattern, new/function)
How does the JavaScript replace() function work?
The replace() function accepts the search term and replacement term as the two parameter values. It searches the entire string for the old text. When it finds the first occurrence of the text, it replaces it with the new substring.
In the example below, we declared a simple string (cars). In the next line, we used the replace() function to replace ‘cars’ with ‘bikes’. When the replace() function searches for the cars, it is an entire string, so it replaces the entire string with a new text (bikes).
let s = "cars"
let n = s.replace("cars", "bikes")
console.log(n);
bikes
JavaScript replace function example
It is a basic example of the string replace() function. Here, we have declared a string variable and assigned a text to it. In the next line, we used the replace() function to replace the word “bye” with a new substring “Good”.
let s = "bye bye!"
let n = s.replace("bye", "Good")
console.log(n);
Good bye!
In the original string, there are two words, and both are the same. However, the replace() function replaced the first word with Good and kept the second one unchanged.
When the replace() function finds the substring, it stops searching at that point and performs the replacement operations without going further. So, if there are N occurrences, the replace() function replaces the first occurrence and leaves the others unchanged.
JavaScript replace() won’t change the original string
As mentioned earlier, the replace() function does not change the original string and returns a new string. We must always assign the result of the replace() function to a new string. Otherwise, the original string remains the same.
let s = "New Year"
s.replace("New", "Good")
console.log(s);
New Year
Fix: Always assign the result to a new string. Otherwise, reassign it to the old string.
let s = "New Year"
s = s.replace("New", "Good")
console.log(s);
Good Year
Empty pattern or first argument
If you pass an empty pattern as the first argument, the JavaScript replace() function considers that you intend to replace the starting position. So, it appends the substring (Second argument) at the beginning of the given text.
In the example below, the first statement appends a dollar symbol to the Hello text. The second statement adds “Hi, ” to the beginning of the text.
let s = "Hello"
console.log(s.replace('', '$'));
console.log(s.replace("", "Hi, "))
$Hello
Hi, Hello
Replacing all occurrences of a string
In the above-mentioned examples, we used the JavaScript replace() function, which replaces the first occurrence of a substring in a given string. However, there are situations where we need to replace all occurrences.
In this script language, there are two options to replace all occurrences of a substring in a main string. The first option is built-in replaceAll() method, and the other option is the replace() function with a regular expression.
Replace all occurrences using replaceAll()
The JavaScript replaceAll() function is a built-in string method to replace all occurrences of an old text with a new substring.
let s = 'Sports bikes are the fastest bikes.';
let n = s.replaceAll("bikes", "cars")
console.log(n);
Sports cars are the fastest cars.
JavaScript replace String Example
This example will help you understand the String replace function. The third statement finds the substring ‘abc’ and substitutes it with ‘JavaScript’. If you observe the screenshot below, although the ‘abc’ substring is repeated twice in Str2, this String function is changing the first occurrence.
In the next line, we used a regular expression to search for the substring ‘abc’ globally. So, this function substitutes every occurrence of the word ‘abc’ with ‘JavaScript‘. Next, using a regular expression gi to perform the case-insensitive search.
<!DOCTYPE html>
<html>
<head>
<title>ReplaceinJavaScript</title>
</head>
<body>
<h1>JavaScriptReplace</h1>
<script>
var Str1 = "We are ABC working at abc company";
var Str2 = "We are abc working at abc company";
var Str3 = Str2.replace("abc","JavaScript");
var Str4 = Str2.replace(/abc/g,"JavaScript");
var Str5 = Str1.replace("abc","JavaScript");
var Str6 = Str1.replace(/abc/gi,"JavaScript");
document.write(Str3 + "<br \>");
document.write(Str4 + "<br \>");
document.write(Str5 + "<br \>");
document.write(Str6 + "<br \>");
</script>
</body>
</html>

Use the JavaScript replace function with regex
The other option is to use a regular expression with a global flag g as the first argument of the replace() function. The following example replaces all occurrences of the word bikes in a given string with cars.
let s = 'Sports bikes are the fastest bikes.';
let n = s.replace(/bikes/g, "cars")
console.log(n);
Sports cars are the fastest cars.
Case-Insensitive replacement
By default, the JavaScript replace() function is case-sensitive, meaning it treats Apple and apple as two different entries. Therefore, when working with the string replacement, we must be careful with lowercase and uppercase characters. Otherwise, use the i flag along with the g flag to perform a case-insensitive search.
- g to perform a global search and replacement.
- i means ignore case flag to treat lower and uppercase characters as the same.
let text = 'Ferrari Cars are Faster cars';
let str = text.replace(/cars/gi, "Bikes");
console.log(str);
Ferrari Bikes are Faster Bikes
If we remove the i flag in the example above
text.replace(/cars/g, "Bikes");
The result becomes
Ferrari Cars are Faster Bikes
Replacing multiple spaces
The following example removes the extra spaces between the words.
let s = 'New York';
let n = s.replace(/\s+/g, ' ');
console.log(n);
New York
JavaScript replace function on Digits
We can use a regular expression to search for digits in a given string and replace them with another substring. In the example below, \d represents any digit (single), and g stands for global search.
The combination of both searches for all occurrences of a digit in a given string and replaces them with N. If we change 9 to 10, it shows NN instead of N because each digit is replaced with N. The number 10 represents two digits.
let text = 'The store contains 9 Apples and 7 Oranges';
let str = text.replace(/\d/g, 'N');
console.log(str);
The store contains N Apples and N Oranges
Similarly, in the following example, d{3} means replace three digits with three stars.
let text = '202-555-9147';
let str = text.replace(/\d{3}/g, '***');
console.log(str);
***-***-***7
Replace the complete word
In general, when we apply the JavaScript replace() function with a regular expression, it’ll replace all occurrences of a search field within a given substring. For instance, in the example below, it replaces all occurrences of the word car with bike.
let s = 'car scar in carrier';
let n = s.replace(/car/g, 'bike');
console.log(n);
bike sbike in bikerier
There are situations where we replace the complete word with the given substring. Instead of substituting a portion of a word (a few characters within a single word), it should check for the exact match in the whole world and replace it with them.
For instance, in the above example, we only need to replace the car with a bike. The replace function should leave the scar and carrier as they are because they are not the whole world. Car is a portion of scar and carrier words.
let s = 'car scar in carrier';
let n = s.replace(/\bcar\b/g, 'bike');
console.log(n);
bike scar in carrier
Handle Special Characters in Regular Expressions
When working with the JavaScript replace() function and a regular expression, we must be careful when handling special characters. In regular expressions, special characters like *, ., +, ?, ^, $, {, }, (, ), |, [, ], and \ has a special meaning. So, we must use the escape character (\) to escape those special characters.
In the example below, we use the escape character to replace the | symbol with the * symbol.
let text = 'a|b|c';
let str = text.replace(/\|/g, '*');
console.log(str);
a*b*c
Special Character using Unicode
Apart from the above, we can use the Unicode character to escape a particular special character, something like symbols.
let text = 'Latte☕!';
let str = text.replace(/\u2615/g, ' Coffee');
console.log(str);
Latte Coffee!
Removing Symbols or special characters in a string
The above-mentioned approaches deal with special characters that have a special meaning in regular expressions. However, we can use the approach below to remove unwanted special characters in a given string. For example, if a string contains unwanted @, #, $, and other symbols, we can use the JavaScript replace function with a regular expression to remove them.
In the following example, we used the ^ symbol for negation followed by a range of letters and numbers. It means anything apart from the lowercase and uppercase alphabets and numbers from 0 to 9 is removed from a given string.
let text = '@User:1234! $Log$ &In!';
let str = text.replace(/[^a-zA-Z0-9]/g, "");
console.log(str);
User1234LogIn
Replacing Line Breaks
We can use the JavaScript replace function() with a regular expression to remove the line breaks caused by the newline (\n) character.
let text = 'New\nYork';
let str = text.replace(/\n/g, ' ');
console.log(str);
New York
Replacing Carriage Return
In the following example, we replace the new line (\n) and a carriage return (\r) with a blank space.
let text = 'New\nYork\r\nCalifornia';
let str = text.replace(/\r?\n/g, ' ');
console.log(str);
New York California
Replacing Line Breaks with <br>
let text = 'New\nYork\nCalifornia';
let str = text.replace(/\n/g, "<br");
document.getElementById('Result').innerHTML = str;
Replacing non-ASCII characters
To deal with the non-ASCII characters, we must use the JavaScript replace function with a regular expression. In the first argument, the regular expression must be set for the ASCII character range with negation. It means anything outside the ASCII values range is removed or replaced by none.
In the example below, [^\x00-\x7F] is the range of the ASCII characters. Here, the ^ symbol ignores these characters. So, it ignores emojis, other language characters (Chinese Hello), and accented letters.
const text = '你好!Welcome to Café 😊';
const str = text.replace(/[^\x00-\x7F]/g, '');
console.log(str);
Welcome to Caf
JavaScript replace multiple words
To replace multiple substrings in an original string with a single text, we can use the logical OR operator inside a regular expression.
The following example searches for the cars and Bikes inside a given string. If the replace() function finds them, it replaces them with the Motorcycle.
const text = 'BMW Cars and Benz Bikes';
const str = text.replace(/Cars|Bikes/g, 'Motorcycle');
console.log(str);
BMW Motorcycle and Benz Motorcycle
The above example is well-suited to replace multiple words with a single text. However, to replace multiple words, we must use a different strategy.
The following example uses the callback function approach to replace the matching word with the text inside the changes.
const text = 'BMW Cars and Benz Bikes';
const changes = {
BMW: 'Red Bull',
Benz: 'Yamaha',
};
const str = text.replace(/BMW|Benz/g,
match =>changes[match]);
console.log(str);
Red Bull Cars and Yamaha Bikes
Handling International Text
The JavaScript replace() function supports international languages. It means we can use the replace() function to search for other languages’ text and replace it with a message in another language.
The original string in the following query is a combination of Chinese and Japanese languages. The replace () function replaces the Chinese hello word with the English term.
const text = '你好ワールド';
const str = text.replace('你好', 'Hello');
console.log(str);
Helloワールド
JavaScript replace function: Lookahead and Lookbehind
Lookahead means checks what comes after the given search item. Similarly, lookbehind means checking what comes before the matching substring.
Positive Lookahead (?=..)
In the following example, we are searching for a Bike and the word must be followed by an s character. Here, three words matched with the Bike. However, only one word matched the expression because Bikes = Bike followed by s.
const text = 'Bike: Bikers Rule the Bikes ';
const result = text.replace(/Bike(?=s)/g, 'Race');
console.log(result);
Bike: Bikers Rule the Races
If we change the expression as /Bike(?=r)/g, the result becomes the following
Bike: Racers Rule the Bikes
JavaScript replace function Negative lookahead (?!..)
The following example searches for the bike keyword not followed by s word. It means both Bike and Bikers matches because Bike has no s and Bikers (Bike followed by r)
const text = 'Bike: Bikers Rule the Bikes ';
const result = text.replace(/Bike(?!s)/g, 'Race');
console.log(result);
Race: Racers Rule the Bikes
JavaScript replace function Positive lookbehind(?<=..)
The following example demonstrates the lookbehind approach. Here, it looks behind the @ symbol. The text must contain the @ symbol before it. It means the search pattern matches AdventureWorks because it is a combination of more than one character, and it has the @ symbol before it. So, replace it with the domain word.
const text = 'john@adventureworks.com';
const str = text.replace(/(?<=@)\w+/g, 'domain');
console.log(str);
john@domain.com
Negative lookbehind(?<!…)
The example below uses the (?<!) negative lookbehind to search for numbers not having a dollar, rupee, or euro symbol. If they find any match, replace those digits with X.
const text = '$100 ₹200 €300 400 ₹500 600';
const str = text.replace(/(?<![$€₹])\b\d+\b/g, 'X');
console.log(str);
$100 ₹200 €300 X ₹500 X
JavaScript replace() with a function argument
Apart from the simple substring as the replacement text, we can use the function to replace one or all occurrences of a substring in a given string. To perform a more complex replacement or conditional-based string replacement, we can use the function as the second parameter.
A basic syntax of the replace() function with a function argument is shown below.
str.replace(old, function)
In the above JavaScript replace function syntax,
- Old: It can be a string or a regular expression. The replace function searches for this substring inside the string object.
- A function called for every match. Here, you can define a function or call the existing function. When you use the function as the second parameter, it will replace every occurrence of a substring with the function’s return value.
Basic JavaScript replace() with a function argument example
There are three approaches to using the replace() function with a function argument. The first one is to write a function within the replace() function to perform a certain operation.
In the example below, a regular expression search for the word Cars is performed inside a given string. The function parameter returns ‘Bikes’ as the output. It means the replace() function searches for Cars and replaces that word with Bikes (returned by the function).
let text = 'Ferrari Cars';
let str = text.replace(/Cars/g,
function (match)
{
return 'Bikes';
});
console.log(str);
Ferrari Bikes
Using the inline function
Apart from the above, we can use the inline function to perform the basic operations. In the following example, the JavaScript replace() function uses a regular expression that searches for any word (letters and numbers) and converts it to uppercase.
Here, w means word, and the + symbol is for one or more characters. The c.toUpperCase()) is an inline arrow function to convert each character in a given string to uppercase. Here, the function call occurs for each character. Remember, (c) => c.toUpperCase() is equivalent to
function(c) {
return c.toUpperCase();
}
let text = 'New York';
let str = text.replace(/\w+/g, (c) => c.toUpperCase());
console.log(str);
NEW YORK
Using a separate function
Among the three approaches, using a separate method is the best way to use the JavaScript replace() function with a function argument. Here, we declare or create a separate function that performs either basic or advanced operations. Next, call that function from the replace() method.
In the following example, a regular expression performs a global search for the Cars. On each match, it calls the cb() function, which returns Bikes as the output. The replace() function captures it and replaces Cars with Bikes.
function cb(match) {
return "Cars"
}
let text = 'Blue bikes are stylish bikes.'
let str = text.replace(/bikes/g, cb);
console.log(str);
Blue Cars are stylish Cars.
Function argument Parameters
The complex syntax of the JavaScript replace method with a function argument is shown below. The inside function has five parameters, and the return value is used as the replacement text.
str.replace(oldText, function (match, p1, p2, offset, wholeString) {
return newText;
}
We have already shown the match parameter in the above example. So, we will concentrate on the other parameter values. Before going to the examples, remember the order
Parameters order:
- function(match, offset, fullString)
- function(match, p1, p2, offset, fullString)
Capture Groups (p1, p2)
The following example uses the JavaScript replace function and capture groups to reverse the string words. Here,
- (\w+) is the first group. Here, w means the word and + means one or more characters. The first group contains the text starting from the first character until the word’s end position. (\w+) = Tutorial
- \s represents the space.
- (\w+) is the second group after the space. (\w+) = Gateway
TIP: If you add one more word to the original string, it stays unchanged because it does not belong to any captured group.
When you observe the function parameters,
- match = Tutorial Gateway
- first = Tutorial
- last = Gateway
- return last + ‘ ‘ + first = Gateway Tutorial
function name(match, first, last) {
return last + ' ' + first
}
let text = 'Tutorial Gateway'
let str = text.replace( /(\w+)\s(\w+)/, name);
console.log(str);
Gateway Tutorial
JavaScript replace function offset parameter
The offset parameter returns the starting index position of the current matching substring. In the example below, a string has two words, and the regular expression w+ means one or more characters.
- The first characters in the beginning word (Tutorial) start at the index position 0.
- The first character of the second word (Tutroail) starts at the index position 9.
const text = 'Tutorial Gateway';
text.replace(/\w+/g,
function (match, offset)
{
console.log(`"${match}" starts at ${offset}`);
});
"Tutorial" starts at 0
"Gateway" starts at 9
FullString Parameter
The fullstring or wholestring parameter in the JavaScript replace() function returns the original string. We can use it for the comparison-based replacement, debugging, etc. For instance, we used the fullString parameter, and you can see it shows the original string.
const text = 'Tutorial Gateway';
text.replace(/\w+/g,
function (match, offset, fullString) {
console.log("match:", match);
console.log("fullString:", fullString);
});
match: Tutorial
fullString: Tutorial Gateway
match: Gateway
fullString: Tutorial Gateway
Let me use the fullString to perform conditional-based replacement.
let text = 'Blue Cars are Stylish Cars';
let n = text.replace(/Cars/g,
function (match, offset, fullString)
{
if (fullString.includes('Stylish')) {
return 'PHONES'
}
return match;
});
console.log(n);
Blue PHONES are Stylish PHONES
NOTE: If there is no Stylish work inside the original string, the function won’t replace Cars with PHONES.
JavaScript replace function with full parameter example
In the example below, we use all the available function parameters within one example.
let text = 'John Miller 30';
text.replace(/(\w+)\s(\w+)\s(\d+)/,
function (match, first, last, age, offset, fullString)
{
console.log('match:', match);
console.log('FirstName:', first);
console.log('LastName:', last);
console.log('Age:', age);
console.log('offset:', offset);
console.log('fullString:', fullString);
});
match: John Miller 30
FirstName: John
LastName: Miller
Age: 30
offset: 0
fullString: John Miller 30
JavaScript replace() function: Capturing groups
Capture groups are helpful altering the positions or format the text by changing their positions. In the previous section, we had an example explaining the capture group. In this section, we dive deeper into them.
Please remember the following tokens to understand the capturing groups.
- $1 = First group
- $2 = Second group
- $N = Nth group
- $& = Entire Match
- $’ = After the matching substring
- `$“ = Before the matching substring.
Swapping Two Words
In the following JavaScript replace function example, the original string has two words.
- (\w+) is the first group that contains the word Tutorial.
- (\w+) is the second group that contains the word Gateway.
- $2 means the second group (Gateway).
- $1 means the first group (Tutorial).
- $2 and $1 means Second group + and keyword + first group = Gateway and Tutorial.
let text = 'Tutorial Gateway';
let str = text.replace(/(\w+)\s(\w+)/, '$2 and $1');
console.log(str);
Gateway and Tutorial
Format Date Example
- (\d{4}) = Four digit number as the first group = $3.
- (\d{2}) = Two digit number as the second group = $2.
- (\d{2}) = The last two digit number as the third group = $!.
let date = '2025-12-31';
let newdate = date.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1');
console.log(newdate);
31/12/2025
Named capturing Groups in JavaScript replace function
To demonstrate the same, we used the same example as the above data format. Here, ?<Name> assigns a name to the capturing group. It means the first group name is the year, the second group name month, and the third group is the day.
let date = '2025-12-31';
let newdate = date.replace(
/(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/,
"$<day>/$<month>/$<year>");
console.log(newdate);
31/12/2025
Formatting Phone Number
let text = '202-555-9147';
let str = text.replace(/(\d{3})-(\d{3})-(\d{4})/, '($1) $2-$3');
console.log(str);
(202) 555-9147
JavaScript replace function Patterns
The following table shows the patterns list and their meaning.
| Pattern | Meaning |
|---|---|
| $$ | Inserts a Dollar ($) symbol. |
| $& | Inserts the matching substring. |
| $’ | Text after the match. |
| $` | Inserts the text before the match. |
| $1 | First captured group. |
| $n | Nth captured group. |
| $<Name> | Named captured group. |
Most of the above-mentioned patterns are already covered in the captured group examples. We provide simple examples to understand the patterns.
$$ – Insert a $ symbol
let price = "100"
console.log(price.replace('', '$$'))
$100
$& – Matched substring
The $& symbol stands for the entire match, and it is helpful to add text or wrap the existing text inside [] or <>.
let text = "car carrier scars"
console.log(text.replace(/\bcar\b/g, "Premium $&"));
Premium car carrier scars
$` – Text before the match
The $` returns the text before the matching substring. The following example searches for the word York. When it finds the $` is the text before York, so it returns New New City.
let text = "New York City"
console.log(text.replace('York', '$`'));
New New City
NOTE: If the text before the York word is a sentence of more than one word, the $` symbol repeats the whole sentence.
$’ – Text before the match
The text after the York word is City is Beautiful, so the replace() method replaces the York word with this sentence.
let text = "New York City is Beautiful."
console.log(text.replace('York', "$'"));
New City is Beautiful. City is Beautiful.
NOTE: For the $1, $2, and <name>, please refer to the capturing groups examples.
JavaScript replace function real-time Examples
The following are some of the replace() function examples at the production level.
JavaScript replace function to convert a CSV to an Array
In the following example, the replace() function replaces the carriage return and the newline character with \n. The split() function splits the string based on the new line character.
It means there are four rows, and each row has the name, age, and country. Next, map((row) => row.split(‘,’)) further splits those items based on the comma delimiter.
const csv = 'name,age,country\r\nTracy,30,UK\r\nShah,27,UAE\r\nTim, 29, USA';
const converted = csv.replace(/\r\n/g, '\n');
const result = converted.split('\n').
map((row) => row.split(','));
console.log(result);
[
[ 'name', 'age', 'country' ],
[ 'Tracy', '30', 'UK' ],
[ 'Shah', '27', 'UAE' ],
[ 'Tim', ' 29', ' USA' ]
]
Add Commas to Numbers
In the example below, we used the positive and negative lookahead to add commas to numbers.
let n = '50000000';
const result = n.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(result);
50,000,000
Hiding Bad Information
In the example below, a regular expression search for the bad words and the arrow function replaces them with a star symbol. Here, the length function helps to add the starts based on the total number of characters in a word.
let text = 'Good Bad Better Ugly Nice Brutal';
const str = text.replace(
/Bad|Ugly|Brutal/g, (word) => '*'.repeat(word.length),
);
console.log(str);
Good *** Better **** Nice ******
Mask Credit Card Information
The following JavaScript replace function example masks or replaces the first 12 digits of a given credit card number, displaying the last four digits. It simply replaces the first 12 digits with star symbols using the positive lookahead approach (?=).
function maskCard(number)
{
return number.replace(/\d(?=\d{4})/g, '*');
}
let n = '1234567891239999'
console.log(maskCard(n));
************9999
Create a URL from the Title
The following example converts the given meta title to a URL slug. It removes the special characters, unwanted spaces, and hyphens from the original title and creates a clean URL.
- toLowerCase(): Converts the complete text into lowercase.
- trim(): Removes the unwanted leading and trailing blank spaces.
- replace(/[^\w\s-]/g, ”): Remove everything except letters, numbers, spaces, and hyphens.
- replace(/\s+/g, ‘-‘): Replace spaces with the – symbol.
- replace(/-+/g, ‘-‘): Removes repeated hyphens in a string.
function slug(title) {
return title
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/\s+/g, '-')
.replace(/-+/g, '-');
}
console.log(slug(' Explain: Complete Info on @SERVER Guide!!! '));
explained-complete-info-on-server-guide
Replacing the HTML tags
function deletion(tags)
{
return tags.replace(/<[^>]*>/g, "");
}
let n = '<h1>Title</h1>'
console.log(deletion(n));
Title
Using split() and join() for string replacement
Instead of the JavaScript replace() function, we can use the combination of the split() and join() functions. The following example uses the string replace() function to substitute all occurrences of the word ‘ Cats’ with Dogs
const text = 'Small Cats are more adorable than Big Cats';
const str = text.replace(/Cats/g, 'Dogs');
console.log(str);
Small Dogs are more adorable than Big Dogs
It can be achieved using the split function and the join method. The split() function splits the existing string based on the word Cats and returns an array. Next, the join() function adds Dogs in that empty palce.
const text = 'Small Cats are more adorable than Big Cats';
const str = text.split('Cats').join('Dogs');
console.log(str);
Small Dogs are more adorable than Big Dogs