simplest:
str = str.replace(/-/g, "");
Answer from ehmad11 on Stack OverflowfreeCodeCamp
freecodecamp.org › news › javascript-replace-how-to-replace-a-string-or-substring-in-js
JavaScript Replace – How to Replace a String or Substring in JS
November 7, 2024 - In this example above, the regular expression /\D/g matches all non-digit characters in the originalString. The \D character class matches any character that is not a digit. The g flag specifies that all matches should be replaced. Regular expressions make it possible for you to perform advanced search and replace operations. For example, using the i flag, you can replace only strings and substrings whose case matches perfectly: const originalString = "I love JavaScript and javascript loves me"; const newString = originalString.replace(/JavaScript/i, "Python"); console.log(newString); // Output: "I love Python and javascript loves me"
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript - MDN Web Docs
April 12, 2026 - If it's a string, it will replace the substring matched by pattern. A number of special replacement patterns are supported; see the Specifying a string as the replacement section below. If it's a function, it will be invoked for every match and its return value is used as the replacement text.
How do you replace a character in a string with a single backslash?
r/learnpython is probably a better subreddit for these kinds of questions.
Having said that, your first example actually works, try:
print("apple".replace('l', '\\')) More on reddit.com time complexity of javascript functions
Any toString method where the returned string contains the object's contents in some form obviously can't be O(1) (you can't create a string containing n items in less than O(n) time). A toString method that just returns a fixed string (like Object.prototype.toString, which just returns "[object Object]") would be O(1) though. String.prototype.replace has to at least be O(n) because it has to search through the entire string in the worst place and because it has to create a new string whose length will linear in the length of the original string. And creating a string of length n is of course an O(n) operation. But with a sufficiently complicated regex, the complexity can actually be much worse than O(n) - the complexity of matching regular expressions, as implemented in JavaScript and many other languages at least, can be exponential in pathological cases. More on reddit.com
Q: How can I use a dictionary to replace characters in a string?
635k members in the learnpython community. Subreddit for posting questions and asking for general advice about your python … More on reddit.com
7 Methods To Check If A String Contains A Substring In Javascript
check if a string contains a substring in javascript (in ES6): var I mean... I don't feel lodash or underscore are valid for this article. You're listing ways to check if a string has a substring, not external libraries that check that. Also, #7 has a typo: Syntax of includes method is include(string, substring) Should be includes. More on reddit.com
Videos
01:44
How To Replace Part of a String - JavaScript String Replace (In ...
08:46
replace and replaceAll methods | String Object In JavaScript - YouTube
13:34
JavaScript Replace Method Explained: Strings, Regex, & Dynamic ...
04:42
String Replace with Callback Function, in JavaScript - YouTube
09:20
Using the String.replace() method - JavaScript Tutorial - YouTube
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.replace()
JavaScript String replace() Method
November 4, 2024 - let newStr = str.replace(substr ... In this syntax, the replace() method calls the replacer function after the match has been performed and then uses the result of the function as the replacement string....
Mimo
mimo.org › glossary › javascript › replace
JavaScript Replace Method: Advanced String Manipulation
The replace() method in JavaScript is a powerful tool for string manipulation. It allows you to replace a substring within a string with another.
GeeksforGeeks
geeksforgeeks.org › javascript › replace-all-occurrences-of-a-substring-in-a-string-in-javascript
Replace all Occurrences of a Substring in a String in JavaScript - GeeksforGeeks
August 5, 2025 - Next, we can break the string from that index using str.slice() and insert the replacement string to get the required output. str.indexOf(searchValue, startIndex); string.slice(startingIndex, endingIndex); ... let str = "GEEKSforGEEKS"; let ...
Stack Abuse
stackabuse.com › replace-occurrences-of-a-substring-in-string-with-javascript
Replace Occurrences of a Substring in String with JavaScript
September 25, 2020 - It accepts a regular expression as the first argument, and the word(s) we're replacing the old ones with as the second argument. Alternatively, it accepts a string as the first argument too. Since strings are immutable in JavaScript, this operation returns a new string.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › substring
String.prototype.substring() - JavaScript - MDN Web Docs
It will replace both individual characters and substrings. The function call at the end of the example creates a string Brave New Web from the original string Brave New World.
Codegive
codegive.com › blog › js_replace_substring_in_string.php
Mastering js replace substring in string: Your Ultimate Guide to Dynamic Text Manipulation
... Formatting text for display5. Common mistakes ... At its core, js replace substring in string refers to the process of finding a specific sequence of characters (a substring) within a larger string and substituting it with a different sequence of characters.
W3Schools
w3schools.com › jsref › jsref_replace.asp
JavaScript String replace() Method
The replace() method searches a string for a value or a regular expression.
CoreUI
coreui.io › blog › how-to-replace-all-occurrences-of-a-string-in-javascript
How to replace all occurrences of a string in JavaScript? · CoreUI
August 31, 2024 - The replaceAll method searches for all instances of the search pattern and replaces them with the new substring, making it a straightforward solution. However, it’s important to note that replaceAll is not supported in older browsers. When dealing with large strings or performance-critical code, consider the following: Regular expressions are powerful but may introduce overhead. split and join are simple and effective for straightforward replacements. replaceAll is the most user-friendly but requires compatibility with modern JavaScript environments.
Medium
medium.com › @python-javascript-php-html-css › mastering-string-replacement-in-javascript-41dc9e39665e
Learning JavaScript String Replacement | by Denis Bélanger
August 24, 2024 - JavaScript provides several methods to achieve this, but understanding the nuances of each approach is key to applying them effectively. The traditional method for replacing all occurrences of a substring involves the use of the `String.prototype.replace()` method combined with a regular expression.
TutorialsPoint
tutorialspoint.com › typescript › typescript_string_replace.htm
TypeScript - String replace()
function − A function to be invoked to create the new substring. flags − A String containing any combination of the RegExp flags: g · It simply returns a new changed string. var re = /apples/gi; var str = "Apples are round, and apples are juicy."; var newstr = str.replace(re, "oranges"); console.log(newstr) On compiling, it will generate the same code in JavaScript.
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-replace-a-portion-of-strings-with-another-value-in-javascript
How to replace a portion of strings with another value in JavaScript ? - GeeksforGeeks
May 27, 2024 - <script> let string = "GeeksForGeeks"; ... functions together to do this task. The split function is a javascript inbuilt function that is used to split a string into an array of substrings....
Code.mu
code.mu › en › javascript › manual › regular › replace
The replace method - replacement of string elements in JavaScript
The replace method searches for and replaces parts of a string. The first parameter is regular expression, and the second is the substring to replace with.