Why don't you do this with javascript which is very simple using the code below
which i have edited to get variable displayed in a text box
source: Remove spaces in a Javascript variable
<input type="text" id="mytext" name="mytext" value="">
<script>
var a = "A344 34";
a=a.replace(/\s/g, "");
alert(a);
var test = "Hello";
document.getElementById("mytext").value = a;
</script>
also if you insist on using jquery, then similar solution has been implemented by @VSri58 as per the code below
H el lo
$("#test").on('click', function(){
//var songToTrim = $(this).html();
var songToTrim = 'A344 34';
var trm = songToTrim.replace(/ /g,'');
// try this
document.getElementById("mytext1").value = trm;
alert(trm);
var songToPlay = trm.toLowerCase();
});
you can customize the code to suit your need
source for the above code: Jquery Trim all spaces from variable
Masking Example
var value = "1234567"
var formatted = value.replace(/^(\d{3})(\d{4}).*/,"$1-$2");
alert(formatted);
source: Mask javascript variable value