-2

How can I remove "-" from a whole string?

I am using this line which works but only for the first "-", it doesn't remove the other "-".

image name: cropped-Journal-trend-02.png

 imagePath: $(this).find('td:eq(3)').html().replace(('-', '')
Adriano
  • 3,788
  • 5
  • 32
  • 53
  • var imagePath = "cropped-Journal-trend-02.png"; var newImagepath = imagePath .replace(new RegExp('-', 'g'),""); Can you pls try this. It will remove all - from your string – user6044627 Oct 17 '18 at 06:06

3 Answers3

1

You can add g modifier that is global.It does not return after first match

imagePath: $(this).find('td:eq(3)').html().replace(/-/g, '')
brk
  • 48,835
  • 10
  • 56
  • 78
1
imagePath: $(this).find('td:eq(3)').html().replace(/-/g, '')
Jordan
  • 176
  • 10
0
var imagePath = "cropped-Journal-trend-02.png";
var newImagepath = imagePath.replace(new RegExp('-', 'g'),"");

Can you pls try this. It will remove all - from your string

user6044627
  • 100
  • 11
  • i also try this not remove.@user6044627 – Journal Trends Oct 17 '18 at 06:41
  • It will work. Open your developer tool and paste var imagePath = "cropped-Journal-trend-02.png"; var newImagepath = imagePath.replace(new RegExp('-', 'g'),""); print newImagepath . This will not have any - sign. – user6044627 Oct 17 '18 at 06:54