Javascript: replace multiple identical parts in a string with different strings

javascript replace multiple matches
string replace multiple python
string.replace multiple c#
string replace multiple java
javascript remove part of string
javascript replace case insensitive
html string replace
replace character in string

I am working on a "fill in the blanks" card game, (very) similar to Cards Against Humanity. In some cases, I want to update a string of text with multiple blanks ("My ______ is ______.", for example), but I'm having trouble replacing these identical blank fields with different strings. Let's say I'd like to turn my string into "My cat is white". How could I possibly make this happen? I can replace every blank space in my strings with the same piece of text, but I can't seem to alternate between them. I guess I could cut my string into pieces every time a blank space ends, replacing all fragments with the text I want, and then joining the strings together... but is there a simpler way?

Thanks in advance!


You may try internally using placeholders in your input string. That is, consider using the following representation:

My {1} is {2}.

When you want to display the original version you had to your users, simply replace the {1}, {2}, etc. placeholders with underscores. Then, when you need to fill in a particular blank, target the placeholder directly.

var input = "My {1} is {2}.";
var display = input.replace(/\{\d+\}/g, "______");
console.log(display);
var result = input.replace("{1}", "cat");
result = result.replace("{2}", "white");
console.log(result);

Replace multiple characters in one replace call, () in order, so if the first new contains the following old , the first new is also replaced. JavaScript | Split a string with multiple separators Given a statement which contains the string and separators, the task is to split the string into substring. String split() Method : The str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the


An alternative is using this regexp _{1,6} without the global flag g.

The call of the function String.prototype.replace(regexpWithNoG, string) will replace only the first match.

let str = "My ______ is ______.";
    words = ["cat", "white"];

words.forEach(w => str = str.replace(/_{1,6}/, w));
console.log(str);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Replace strings in Python (replace, translate, re.sub, re.subn), How do you replace a single character in a string JavaScript? String Primitives and String Objects. First, we will clarify the two types of strings. JavaScript differentiates between the string primitive, an immutable datatype, and the String object. In order to test the difference between the two, we will initialize a string primitive and a string object.


This is a function to get that result.

function replace(str,values){
  var parts=str.split(/[_]+/g)
  var result = []
  result.push(parts[0])
  for (var i=1; i<parts.length; i++){
    result.push(values[i-1])
    result.push(parts[i])
  }
  return result.join("")
}

Example:

alert(replace("asdad ___ asdasd ___ aaa",[1,2]))

Returns:

 "asdad 1 asdasd 2 aaa"

https://codepen.io/anon/pen/BqBKbM?editors=1010

How to Replace Character Inside a String in JavaScript, all the occurrence you can use the global ( g ) modifier. A string is a sequence of one or more characters that may consist of letters, numbers, or symbols. Strings in JavaScript are primitive data types and immutable, which means they are unchanging. As strings are the way we display and work with text, and


JavaScript, Replacing strings in JavaScript is a fairly common task, but there are some The most obvious way to do string replacement is by passing 2 strings to replace() , the string to For example, replacing multiple words at once. The following looks for 3 digit numbers and replaces them with the same number,  In JavaScript, the textual data is stored as strings. There is no separate type for a single character. The internal format for strings is always UTF-16 , it is not tied to the page encoding.


JavaScript String Replace Magic / Coder's Block, JavaScript | Replace multiple strings with multiple other strings. replace() method. This method searches a string for a defined value, or a regular expression, and returns a new string with the replaced defined value. Syntax: JavaScript Array join() Method. This method adds the elements of an array into a string, and The replace() method searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced. Note: If you are replacing a value (and not a regular expression), only the first instance of the value will be replaced. To replace all occurrences of a specified value, use the global (g) modifier (see "More Examples" below).


JavaScript, Given a string and a pattern, replace multiple occurrences of a pattern by Since pattern length is always more than equal to 1 and replacement is only 1 See your article appearing on the GeeksforGeeks main page and help other Geeks. Sort the strings based on the numbers of matchsticks required to represent them​  Hello! I am trying to replace multiple characters in a string using only one line of command. I am aware I can use regexprep to replace multiple characters with a single character like: