function NoDoublesPls3()
{ var str=document.getElementById("NoDoubles3");
var regex=/[^a-z 0-9]/gi;
str.value=str.value.replace(regex ,"") .replace(/\s\s+/g, " ");
}
// NOTE the possible initial and trailing spaces
var str = " The dog has a long tail, and it is RED! "
str = str.replace(/^\s+|\s+$|\s+(?=\s)/g, "");
// str -> "The dog has a long tail, and it is RED !"
<script type="text/javascript">
var myStr = "The dog has a long tail, and it is RED!";
alert(myStr); // Output 'The dog has a long tail, and it is RED!'
var newStr = myStr.replace(/ +/g, ' ');
alert(newStr); // Output 'The dog has a long tail, and it is RED!'
</script>
// Trims & replaces any wihtespacing to single space between words
String.prototype.clearExtraSpace = function(){
var _trimLeft = /^\s+/,
_trimRight = /\s+$/,
_multiple = /\s+/g;
return this.replace(_trimLeft, '').replace(_trimRight, '').replace(_multiple, ' ');
};
let nameCorrection = function (str) {
let strPerfect = str.replace(/\s+/g, " ").trim();
let strSmall = strPerfect.toLowerCase();
let arrSmall = strSmall.split(" ");
let arrCapital = [];
for (let x of arrSmall.values()) {
arrCapital.push(x[0].toUpperCase() + x.slice(1));
}
let result = arrCapital.join(" ");
console.log(result);
};
nameCorrection("Pradeep kumar dHital");