अतिरिक्त स्पेस हटाने के लिए, आपको trim() के साथ रेगुलर एक्सप्रेशन का उपयोग करना होगा। trim() लागू करने से पहले स्पेस के साथ हमारा स्ट्रिंग:
var sentence="My name is John Smith ";
अब, हम नीचे दिए गए कोड में अतिरिक्त स्पेस हटाएंगे:
उदाहरण
var sentence="My name is John Smith "; console.log("The actual value="); console.log(sentence); var originalSentence = sentence.replace(/\s+/g,'').trim(); var afterRemovingTheSpace=originalSentence.trim(); console.log("After removing the spaces="); console.log(afterRemovingTheSpace);
उपरोक्त प्रोग्राम चलाने के लिए, आपको निम्न कमांड का उपयोग करना होगा:
node fileName.js.
यहां, मेरी फाइल का नाम demo90.js है।
आउटपुट
यह निम्न आउटपुट उत्पन्न करेगा:
PS C:\Users\Amit\JavaScript-code> node demo90.js The actual value= My name is John Smith After removing the spaces= MynameisJohnSmith