-1

I'm trying to replace every word in a text for another but couldn't find any of for doing that.

Like this example:

from this> hi my name is Robert.

to this> ligh ligh ligh ligh ligh (same amount of words)

Can someone help me? Thanks in advance.

CRice
  • 29,968
  • 4
  • 57
  • 70

1 Answers1

2

Just use String.prototype.replace:

var from = 'hi my name is Robert.';
var to = from.replace(/([a-zA-Z]+)/g, 'ligh'); 

// or use `[a-zA-Z0-9]` if you want to allow numbers in a word
var to2 = from.replace(/([a-zA-Z0-9]+)/g, 'ligh'); 

console.log(to);
Martin Adámek
  • 16,771
  • 5
  • 45
  • 64