Javascript replace all functions

Javascript: Replace all occurrences of string (4 ways)

This article talks about how to replace all occurrences of a string in javascript using different methods and various examples.

Table of Contents:-

Replace all occurrences of string case-insensitive in javascript using replace()

The replace() method in javascript looks for a pattern and replaces some or all of its occurrences with a replacement(string) . The pattern can be a string or regExp.

replace(regexp, newSubString) replace(subString, newSubString)

Frequently Asked:

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based." dummyString = dummyString.replace(new RegExp("this", "gi"), "Javascript") console.log(dummyString)
let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based." dummyString = dummyString.replace(new RegExp("This", "gi"), "Javascript") console.log(dummyString)
Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

Here in the replace() function, an object of RegExp is passed with “this” -> first argument, , “gi” -> second argument to identify the pattern “this”. “gi” specifies that the replacement should be case insensitive.

Читайте также:  Add logs in java

Replace all occurrences of string case insensitive in javascript using replaceAll()

The replaceAll() method in javascript returns a new string with all matches of a pattern replaced by a replacement(string).

replaceAll(regexp, newSubString) replaceAll(subString, newSubString)

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based." dummyString = dummyString.replaceAll(/this/gi, "Javascript") console.log(dummyString)
Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

Replace all occurrences of string in javascript using split and join

This section will replace all occurrences of a string in javascript by splitting and joining an array. The solution is to split the original string by the search string and then replace them with a new substring. Then, finally, joining back into one string.

newString = originalString.split(subString).join(newSubString)

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based." dummyString = dummyString.split("This").join("Javascript"); console.log(dummyString)
Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

Replace all occurrences of string in javascript using substring and indexOf

In this section, we will replace all occurrences of a string in javascript by:

  • Finding the indexOf of a pattern.
  • Identifying the substring to be replaced based on the index and then replacing it with the new substring.
  • Finally, concatenating the substrings to one string.

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based." let i=-1; let findToken = 'This' let newSubString ='Javascript' while (( i=dummyString.indexOf(findToken,i>=0? i=newSubString.length : 0 )) !== -1 ) < dummyString= dummyString.substring(0,i) + newSubString + dummyString.substring(i+findToken.length) >console.log(dummyString)
Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

We hope this article helped you to replace all occurrences of a string in javascript. Good Luck .

Share your love

Leave a Comment Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Terms of Use

Disclaimer

Copyright © 2023 thisPointer

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.

The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.

The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.

The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.

Источник

String.prototype.replaceAll()

The replaceAll() method returns a new string with all matches of a pattern replaced by a replacement . The pattern can be a string or a RegExp , and the replacement can be a string or a function to be called for each match. The original string is left unchanged.

Try it

Syntax

replaceAll(pattern, replacement) 

Parameters

Can be a string or an object with a Symbol.replace method — the typical example being a regular expression. Any value that doesn’t have the Symbol.replace method will be coerced to a string.

If pattern is a regex, then it must have the global ( g ) flag set, or a TypeError is thrown.

Can be a string or a function. The replacement has the same semantics as that of String.prototype.replace() .

Return value

A new string, with all matches of a pattern replaced by a replacement.

Exceptions

Thrown if the pattern is a regex that does not have the global ( g ) flag set (its flags property does not contain «g» ).

Description

This method does not mutate the string value it’s called on. It returns a new string.

Unlike replace() , this method would replace all occurrences of a string, not just the first one. This is especially useful if the string is not statically known, as calling the RegExp() constructor without escaping special characters may unintentionally change its semantics.

function unsafeRedactName(text, name)  return text.replace(new RegExp(name, "g"), "[REDACTED]"); > function safeRedactName(text, name)  return text.replaceAll(name, "[REDACTED]"); > const report = "A hacker called ha.*er used special characters in their name to breach the system."; console.log(unsafeRedactName(report, "ha.*er")); // "A [REDACTED]s in their name to breach the system." console.log(safeRedactName(report, "ha.*er")); // "A hacker called [REDACTED] used special characters in their name to breach the system." 

If pattern is an object with a Symbol.replace method (including RegExp objects), that method is called with the target string and replacement as arguments. Its return value becomes the return value of replaceAll() . In this case the behavior of replaceAll() is entirely encoded by the @@replace method, and therefore will have the same result as replace() (apart from the extra input validation that the regex is global).

If the pattern is an empty string, the replacement will be inserted in between every UTF-16 code unit, similar to split() behavior.

For more information about how regex properties (especially the sticky flag) interact with replaceAll() , see RegExp.prototype[@@replace]() .

Источник

Оцените статью