sameold
sameold

Reputation: 19242

multiple replace of a letter

I have a string variable var str = 'this is eeeeeeee';. I want to replace each letter e in this string. When I do str.replace('e', 'E'); it does it only to the first e

Upvotes: 1

Views: 163

Answers (4)

Kakashi
Kakashi

Reputation: 2195

Try this:

str.replace('e', 'E', 'g');

Upvotes: 0

epascarello
epascarello

Reputation: 207501

You need to use the global match g with a regular expression

str.replace(/e/g, 'E');

Upvotes: 2

Bartek
Bartek

Reputation: 15599

You can use regular expressions to indicate you want to replace more than one instance. In this case you would use the g flag.

'this is eeeeeeeee'.replace(/e/g, 'E')

More info can be found at replace - MDN

Upvotes: 6

kprobst
kprobst

Reputation: 16651

Try this:

mystr = mystr.replace(/e/g,"E");

Upvotes: 2

Related Questions