Fastest method to replace all instances of a character in a string
Posted By: Anonymous
What is the fastest way to replace all instances of a string/character in a string in JavaScript? A while
, a for
-loop, a regular expression?
Solution
The easiest would be to use a regular expression with g
flag to replace all instances:
str.replace(/foo/g, "bar")
This will replace all occurrences of foo
with bar
in the string str
. If you just have a string, you can convert it to a RegExp object like this:
var pattern = "foobar",
re = new RegExp(pattern, "g");
Answered By: Anonymous
Related Articles
- What is the origin of foo and bar?
- Reference - What does this regex mean?
- Getting infinite loop after entering 2 objects to…
- How to stretch a table over multiple pages
- What does this symbol mean in JavaScript?
- Callback functions in C++
- Usage of __slots__?
- Validate that a string is a positive integer
- How to check if a string contains text from an array…
- How do you access the matched groups in a JavaScript…
- PHP Fatal error: Using $this when not in object context
- Fastest way to list all primes below N
- How does PHP 'foreach' actually work?
- Generate sequence of dates for given frequency as…
- What are the undocumented features and limitations…
- How to print a string at a fixed width?
- How to convert number to words in java
- How to use Regular Expressions (Regex) in Microsoft…
- What is your most productive shortcut with Vim?
- python 3.2 UnicodeEncodeError: 'charmap' codec can't…
- problem with client server unix domain stream…
- Wait until flag=true
- Difference between variable declaration syntaxes in…
- For-each over an array in JavaScript
- Best way to replace multiple characters in a string?
- Is this request generated by EF Core buggy or is it my code?
- shell script. how to extract string using regular…
- How to grep (search) committed code in the Git history
- Vim auto-indenting xml strangely
- A variable modified inside a while loop is not remembered
- Creating an instance of class
- Remove accents/diacritics in a string in JavaScript
- Can't understand the difference between declaring a…
- Sending command line arguments to npm script
- Change column type in pandas
- Reversing a string in C
- Convenient C++ struct initialisation
- Fastest way to check a string contain another…
- pandas: best way to select all columns whose names…
- How to submit a form in Vue, redirect to a new route…
- Polymer Lifecycle: Why are properties not yet loaded…
- Is there some way to test my grammar which refer to…
- jQuery Add class to element if data attribute…
- "Correct" way to specifiy optional arguments in R functions
- pandas.DataFrame.from_dict faster alternative
- Go build: "Cannot find package" (even though GOPATH is set)
- Class vs. static method in JavaScript
- Get the name of an object's type
- What does "Fatal error: Unexpectedly found nil while…
- How to do a regular expression replace in MySQL?
- How to sort an array in descending order in Ruby
- What's the difference between eval, exec, and compile?
- How to use Servlets and Ajax?
- Text Progress Bar in the Console
- Fastest way to iterate over all the chars in a String
- How does String substring work in Swift
- Use dynamic (variable) string as regex pattern in JavaScript
- Use %%s% in a SQL statement of a MySQLdb cursor
- How can I create a Promise in TypeScript from a union type
- Sort table rows In Bootstrap
- Renaming column names of a DataFrame in Spark Scala
- Why does C++ code for testing the Collatz conjecture…
- Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'?,./
- How to have click event ONLY fire on parent DIV, not…
- How can I manually compile a svelte component down…
- Regex lookahead, lookbehind and atomic groups
- How can I access and process nested objects, arrays or JSON?
- How to create a numpy array of arbitrary length strings?
- RegEx match open tags except XHTML self-contained tags
- String concatenation with Groovy
- How to validate an email address using a regular expression?
- Replace multiple strings with multiple other strings
- Is there a version of JavaScript's String.indexOf()…
- How to use Rust nom to write a parser for this kind…
- JQuery, Spring MVC @RequestBody and JSON - making it…
- How can I use inverse or negative wildcards when…
- How to check if all elements of a list matches a condition?
- Vue.js - prop sync not instant
- What is the scope of variables in JavaScript?
- Beautiful way to remove GET-variables with PHP?
- How can I parse a CSV string with JavaScript, which…
- How do I pass a URL with multiple parameters into a URL?
- Can you pre-load related data such that your…
- Why cat does not work with parameter -0 in xargs?
- VueJS how to update data on selection
- How to generate an openSSL key using a passphrase…
- Why use Optional.of over Optional.ofNullable?
- In Vuex + Jest, how to unit test a getter which is…
- How do you match strings in custom Instruments…
- Make: "nothing to be done for target" when invoking…
- How to generate a random string of a fixed length in Go?
- How to type mutable default arguments
- Iterate over the lines of a string
- How to implement the factory method pattern in C++ correctly
- Understanding PrimeFaces process/update and JSF…
- JavaScript replace/regex
- Current time formatting with Javascript
- I want to keep a long process running in the…
- What's the difference between using "let" and "var"?
- Converting an Object to an array of KV values
Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.