Trim is a useful function available in languages like Java & PHP which removes the leading and traling whitespace(s) from a String. Trim è una funzione utile disponibile in linguaggi come Java e PHP che rimuove il leader e traling spazi bianchi (s) da una stringa. Unfortunately Javascript doesn’t natively provide trim functionality to the String object. Purtroppo il Javascript non nativamente trim fornire funzionalità per l'oggetto String. Fortunately there is a simple solution. Fortunatamente non vi è una soluzione semplice. Place the following code near the top of your Javascript file (or inline script) to add trim() functionality to all String objects. Inserisci il seguente codice nella parte superiore del vostro file JavaScript (o in linea di script) per aggiungere trim () a tutte le funzionalità String oggetti.

 String prototype.trim = function() {     a = this.replace(/^\s+/, '');     return a.replace(/\s+$/, ''); }; Prototype.trim stringa = function () (a = this.replace (/ ^ \ s + /,''); ritorno a.replace (/ \ s + $ /,'');); 

This underlines one of the beauty (or beast depending on who you ask) of Javascript - the ability to add functionality to an existing class, even native classes like String, by accessing its prototype. Ciò sottolinea uno dei bellezza (o bestia a seconda che si chiede) di Javascript - la capacità di aggiungere funzionalità a una classe esistente, anche nativo classi come String, accedendo il suo prototipo.

Now you can use trim() on any String in your code. Ora potete usare trim () su qualsiasi stringa nel codice.