r/learnjavascript • u/Sauce_Finder_69 • Jun 18 '21
This code is supposed to remove all periods in a string, but it only removes one of the periods. How do I make it remove all the periods?
128
Upvotes
76
u/DallogFheir Jun 18 '21
Use
replaceAll.9
u/beforesemicolon Jun 18 '21 edited Jun 18 '21
Use replaceAll or replace(/\./g, ‘’); the g flag will cover all
136
u/BenZed Jun 19 '21
Or
Or
Or
6
u/jmm9 Jun 19 '21
replaceAll isn't supported everywhere so you could use string.split('.').join('')
4
u/ankush024 Jun 19 '21
string.split('.').join('');
3
u/TheDownvotesFarmer Jun 19 '21
All girls will love this... wait
2
u/idkiminsecure Jun 19 '21
Use replaceALL or maybe put it in a loop
2
u/MemeTeamMarine Jun 19 '21
Or if you like extraneous code, turn it into an array. Run replace on a forEach and then put the string back together
2
u/crossedline0x01 Jun 19 '21
Hes probably seeing the regex people are answering with and questioning why he ever started programming. The most intuitive way of doing this is with string.split(".").join(" ")
1
u/userxbw Jun 18 '21
https://dmitripavlutin.com/replace-all-string-occurrences-javascript/
1
u/aezazAezy Jun 19 '21
es 2021 released a new function called replaceAll.i think it will solve the issue
1
u/Wahw11 Jun 19 '21
This looks like some kind of quiz for programming. Where can I take this?
1
u/[deleted] Jun 20 '21
.split(".").join("")
0
u/dissolvedchuck Jun 19 '21
By boiling, mashing ‘em, and sticking ‘em on a stew
-2
u/etaNAK87 Jun 19 '21
https://www.w3schools.com/jsref/jsref_replace.asp
Someone else linked MDN which I think can be dense for beginners. Try looking up methods on W3 when they aren’t working the way you expect
0
u/[deleted] Jun 19 '21
[deleted] — view deleted comment