How do you replace all occurrences in a string in javascript?
Update: Show
It's somewhat late for an update, but since I just stumbled on this question, and noticed that my previous answer is not one I'm happy with. Since the question involved replacing a single word, it's incredible nobody thought of using word boundaries (
This is a simple regex that avoids replacing parts of words in most cases. However, a dash
Basically, this question is the same as the question here: Javascript replace " ' " with " '' " @Mike, check the answer I gave there... regexp isn't the only way to replace multiple occurrences of a subsrting, far from it. Think flexible, think split!
Alternatively, to prevent replacing word parts -which the approved answer will do, too! You can get around this issue using regular expressions that are, I admit, somewhat more complex and as an upshot of that, a tad slower, too:
The output is the same as the accepted answer, however, using the
Oops indeed, this probably isn't what you want. What is, then? IMHO, a regex that only replaces 'cat' conditionally. (ie not part of a word), like so:
My guess is, this meets your needs. It's not fullproof, of course, but it should be enough to get you started. I'd recommend reading some more on these pages. This'll prove useful in perfecting this expression to meet your specific needs. http://www.javascriptkit.com/jsref/regexp.shtml http://www.regular-expressions.info Final addition: Given that this question still gets a lot of views, I thought I might add an example of There's no easy way to replace all string occurrences in JavaScript. Java, which had served an inspiration for JavaScript in the first days, has the In this post, you'll learn how to replace all string occurrences in JavaScript by splitting and joining a string, and Moreover, you'll read about the new proposal string.replaceAll() (at stage 4) that brings the replace all method to JavaScript strings. This is the most convenient approach. 1. Splitting and joining an arrayIf you google how to "replace all string occurrences in JavaScript", most likely the first approach you'd find is to use an intermediate array. Here's how it works:
For example, let's replace all spaces
Then
the pieces Here's a generalized helper function that uses splitting and joining approach:
This approach requires transforming the string into an array, and then back into a string. Let's continue looking for better alternatives. 2. replace() with a global regular expressionThe string method To make the method
Let's replace all occurrences of
The regular expression literal
You can
easily make case insensitive replaces by adding
The regular expression Invoking 2.1 Regular expression from a stringWhen the regular expression is created from a string, you have to escape the characters Because of that, the special characters are a problem when you'd like to make replace all operation. Here's an example:
The above snippet tries to transform the search string Escaping the character Nevertheless, does it worth escaping the search string using a function like escapeRegExp() to be used as a regular expression? Most likely not. 2.2 replace() with a stringIf the first argument
3. replaceAll() methodFinally, the method Let's replace all occurrences of
Note that currently, the method support in browsers is limited, and you might require a polyfill. 3.1 The difference between replaceAll() and replace()The string methods
4. Key takeawayThe primitive approach to replace all occurrences is to split the string into chunks by the search string, the join back the
string placing the replace string between chunks: Another approach is to use Unfortunately, you cannot easily generate regular expressions from a string at runtime, because the special characters of regular expressions have to be escaped. And dealing with a regular expression for a simple replacement of strings is overwhelming. Finally, the new string method
My recommendation is to use What other ways to replace all string occurrences do you know? Please share in a comment below! How do you replace all values in a string?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.
How do you replace all occurrences of a character in a string in TypeScript?To replace all occurrences of a string in TypeScript, use the replace() method, passing it a regular expression with the g (global search) flag. For example, str. replace(/old/g, 'new') returns a new string where all occurrences of old are replaced with new .
How do you remove all occurrences of a substring from a string in JavaScript?To remove all occurrences of a substring from a string, call the replaceAll() method on the string, passing it the substring as the first parameter and an empty string as the second. The replaceAll method will return a new string, where all occurrences of the substring are removed.
How do you remove all occurrences of a character from a string in JavaScript?Delete all occurrences of a character in javascript string using replaceAll() The replaceAll() method in javascript replaces all the occurrences of a particular character or string in the calling string. The first argument: is the character or the string to be searched within the calling string and replaced.
|