If you use Str. Replace ("-","! ") Will only Replace the first matched character.
Replace ()
The Replace () method returns the string that results when you replace text matching its first argument
(A regular expression) With The text of the second argument (a string ).
If the G (global) flag is not set In The regular expression declaration, This Method replaces only the first
Occurrence of the pattern. For example,
VaR S = " Hello. regexps are fun. " ; S = S. Replace ( / \. / , " ! " ); // Replace first period with an exclamation pointalert (s );
Produces the string "Hello ! Regexps are fun. "including the G flag will cause the interpreter
Perform a global replace, finding and replacing every matching substring. For example,
VaR S = " Hello. regexps are fun. " ; S = S. Replace ( / \. / G, " ! " ); // Replace all periods with exclamation pointsalert (s );
Yields This Result: "Hello ! Regexps are fun ! "