5

Given these two strings:

var a = "November 5, 1916";
var b = "October 5–10, 1592";

I am doing:

b.replace(' ', '').split(' ');
a.replace(' ', '').split(' ');

But I still get a comma and no white space. I need to be able to remove ALL commas from those strings and keep the white space in order to have:

var resultA = "November 5 1916";
var resultB = "October 5–10 1592";

update

I do need the split() afterwards as I need each string in an array.

rob.m
  • 9,843
  • 19
  • 73
  • 162
  • 2
    I'm no js expert, but your calls to `replace` and `split` don't work with commas at all. – Sam Axe Aug 02 '17 at 20:19
  • you probably should have done a search first on this question. Its probably been answered a million times over. Just search string replace. – thomasmeadows Aug 02 '17 at 20:29
  • 1
    Possible duplicate of [How to replace all occurrences of a string in JavaScript?](https://stackoverflow.com/questions/1144783/how-to-replace-all-occurrences-of-a-string-in-javascript) – thomasmeadows Aug 02 '17 at 20:30
  • @MeghanArmes nope. I am splitting to have each single value into an array, but since there are commas or dashes and 5-10 and actually 2 distinct numbers, I'd also have to remove the dash and add white spaces. – rob.m Aug 02 '17 at 20:47

7 Answers7

7

To replace all occurrences of a string with another string, using the following function:

str.replaceAll(',', '');
5

The split() method is not needed since you are not trying to turn the string into an array.

Just to remove all commas straighforwardly:

a.replace(',', '');
b.replace(',', '');
Ren
  • 66
  • 1
  • 5
2

If all you need to do is remove the commas, then you basically have a typo in your example. The first parameter to replace() should be the character(s) you want to remove. In your example, you are removing the spaces and keeping the commas. You need this:

b.replace('/,/g','');
a.replace('/,/g','');
JakeParis
  • 11,056
  • 3
  • 42
  • 65
2

you can do this:

var stringWithoutCommas = stringWithComas.replace(/,/g, ' ');
1

You can use this

var mystring = "this,is,a,test"
mystring.replace(/,/g , " ");

This will replace all the commas with space.

Muhammad Usman
  • 10,039
  • 22
  • 39
1

var a = "November 5, 1916";
var b = "October 5–10, 1592";
var r=/  |,/gi ;
$("#a").text(a.replace( r,""));
$("#b").text(b.replace( r,""));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span id="a"></span>
<hr>
<span id="b"></span>
Farhad Bagherlo
  • 6,725
  • 3
  • 25
  • 47
1

$(document).ready(function() {

  var a = "November 5, 1916";
  var b = "October 5-10, 1592";


  $("#btn").click(function() {
    
    a = a.replace(",", "");
    b = b.replace(",", "");
    var c =  b.replace("-", " ");

    $("#a").text(a);
    $("#b").text(b);
    $("#c").text(c);

  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="a">a</p>
<p id="b">b</p>
<p id="c">c</p>

<button id="btn" type="button"> Click
otc
  • 694
  • 1
  • 9
  • 40
  • 2
    with this same logic, i could remove the dash too 5-10, and add space . This is identical to the accepted answer btw :) – rob.m Aug 02 '17 at 20:51
  • 1
    yea it looks a like a lot of activity has happened by the time i was done typing :) – otc Aug 02 '17 at 20:53
  • 1
    basically I am trying to answer this and each time I get a new date format, I'll add it in the algorithm https://stackoverflow.com/questions/45417870/how-would-you-handle-different-formats-of-dates – rob.m Aug 02 '17 at 20:55
  • 1
    yeah that is tricky you will need to define "templates", maybe regex will work best. Good luck. – otc Aug 02 '17 at 21:04
  • 1
    exactly, thanks. btw way i tried `b.replace("-", "");` but the dash between 5 and 10 stays, why? – rob.m Aug 02 '17 at 21:05
  • Because the '-' that you had is a little bit longer than normal, you can use that but will have to copy paste it. – otc Aug 02 '17 at 21:15
  • different author for the 'split/join' solution :) – otc Aug 02 '17 at 21:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150877/discussion-between-rob-m-and-otc). – rob.m Aug 02 '17 at 21:20