Javascript replace string blank space

Javascript replace string blank space

The following snippet uses a regular expression to replace multiple blank spaces inside a string.

The regular expression is \s+ 

<html><head>
    <title>Replace spazi vuoi multipli</title>
</head>
<body>
<textarea id="txtar"></textarea>
<br>
<input type="button" value="Mostra Selected Value" onclick="SelValue();" /> 

<script language="javascript" type="text/javascript">
 function SelValue(){
    var txt = document.getElementById("txtar");
    var valore = txt.value.replace(/\s+/g," ");
    alert("Value: " + valore)
 }
</script>
</body>
</html>

 Download source javascript_replace_regurar_expresseion.html (437,00 bytes)

Lo snippet seguente utilizza una regular expression per la funzione replace per sostituire in una stringa gli spazi vuoti multipli con un singolo spazio.

La regular expression utilizzata è \s+ 

<html><head>
    <title>Replace spazi vuoi multipli</title>
</head>
<body>
<textarea id="txtar"></textarea>
<br>
<input type="button" value="Mostra Selected Value" onclick="SelValue();" /> 

<script language="javascript" type="text/javascript">
 function SelValue(){
    var txt = document.getElementById("txtar");
    var valore = txt.value.replace(/\s+/g," ");
    alert("Value: " + valore)
 }
</script>
</body>
</html>

 Download source javascript_replace_regurar_expresseion.html (437,00 bytes)

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *