xxxxxxxxxx
function removeSpaces(str) {
// Use a regular expression to remove spaces at the beginning (^) and end ($) of the string
const trimmedStr = str.replace(/^\s+|\s+$/g, '');
return trimmedStr;
}
// Example usage:
const inputString = " Hello, this is a sample string with spaces at the beginning and end. ";
const result = removeSpaces(inputString);
console.log("Original string:", inputString);
console.log("String after removing spaces:", result);