javascript replace only first instance .replace?

by in 0

I have this
 var date = $('#Date').val();
this get the value in the textbox what would look like this
Now I do this on it
var id = 'c_' + date.replace("/", '');
and the result is
It misses the last '/' I don't understand why though.


That is default function in javascript. It replace only first instance

At this situation:

You need to set the to replace globally:
date.replace(new RegExp("/", "g"), '')
// or
date.replace(/\//g, '')
Otherwise only the first occurrence will be replaced.

Leave a Reply