-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
  • 26,396
  • 4
  • 55
  • 62

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
  • 14,491
  • 5
  • 33
  • 52