I am trying to replace all the slashes inside an input \
with /
jquery as follows
// input val -> G:\Ficheros\Documentacion\PROYECTOS\FILES\document.PDF
$("input").focusout(function() {
var replaceSlashes = $(this).val().replace("\\", "/");
$(this).val(replaceSlashes);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" value="G:\Ficheros\Documentacion\PROYECTOS\FILES\document.PDF"/>
But it only replaces the first one \
, and if I do focusout again , the second one is replaced and so on.
The replace function only changes the first occurrence if you pass a string as the first parameter:
But if instead of using a string as the first parameter, you use a regular expression, you can add the parameter
g
at the end that serves to indicate all occurrences.Note that for this you have to remove the quotes, enclose the string between slashes and put the parameter behind the slashes
g
, that is:/\\/g
You would have this that you can try by clicking on
Ejecutar
:More info: