Replace string with multiple strings

user11264446

I have a text where I want to replace all the letters "A" with different values. I have an array ["b", "c", "d"] and I want to replace first found "A" with "b", the second one with "c" and so on. Anyone, please help. Here is what I am trying and what I get

   var str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
		var a = ["b", "c", "d"];
		var c = str.replace(/a/g, a);
		console.log(c);

Nina Scholz

You could take a closure over the index and increment the index and adjust with the lenght of the array.

var str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
    a = ["b", "c", "d"];
    c = str.replace(/a/g, (i => () => a[i++ % a.length])(0));

console.log(c);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Kotlin - Is there a base function to replace multiple strings with multiple strings in a reference String?

Fastest way to replace multiple strings in a huge string

Java String replace multiple strings before Boolean.parseBoolean in Scanner?

How to replace multiple instances of a sub strings in a string using a for loop (in a function)?

Replace multiple strings within string with react jsx element

Replace a single string in file at specific position with multiple new strings

Replace multiple strings in a text file with one string using javascript

Replace multiple strings at once

Check for multiple replace strings

replace multiple strings in python

replace strings in multiple lines

Replace multiple strings or texts

How to replace strings in a string

Replace multiple words in multiple strings

Replace multiple strings in multiple files

Replace multiple strings with multiple other strings

How do I search a string for multiple strings, and then replace them with other strings? (Python)

How to replace multiple strings in python?

Replace multiple strings in a file with Powershell

Replace multiple strings containing $ sign

How to replace multiple strings at once

Perl replace multiple strings in a file

Replace multiple strings in place that match

Matching and replace multiple strings in python

Replace multiple strings in a list with a string in python and get the count of replaced string occurence

Replace multiple strings between two indexes with in strings

Replace multiple strings with different set of mapped strings

Replace strings with longest string in group

Replace a String between two Strings