-2

When I've studied the code given by my professor, I found a line that I do not quite understand.

The code is like this:

textclean = textclean.replace(/ your /g," ");

I think the meaning is to replace your with a space so we can delete it by split(" "); but what is the meaning of /g?

Why can't we use:

textclean = textclean.replace(your, " ");
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Miranda
  • 129
  • 3
  • 12
  • Read the [doc](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) ! – Enissay Feb 02 '16 at 01:24
  • A new programmer may not realize that [Javascript supports regular expression patterns as literals](https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions) or be familiar with regular expressions. I think in that case they could be forgiven for not knowing what to Google for. Your professor should have explained them before using this notation. – DavidS Feb 02 '16 at 01:25

1 Answers1

1

It means replace globally, not only the 1st occurance. If you have "Foo your bar mooo your example" then without the /g only the 1st " your " would be replaced.

Gavriel
  • 18,880
  • 12
  • 68
  • 105