Skip to main content
Kinetic Community

Remove Spaces

How to remove white space from answers.

Resolution

To use this javascript copy the code below and insert it into your Service item.  Call the function with the appropriate parameter to execute it.

/**
* DESCRIPTION Removes leading and trailing white space and trims any multiple spaces in the middle to one space.
* @method trimSpace
* @param qstn {String} The question label of the question to remove spaces.
* @return value of the queston with white spaces removed
*/
function trimSpace(qstn) {
    // get value of the question adn place it into the variable qstn
    qstn = K('field['+qstn+']').value();
    
    // remove white space (tabs and line returns)
    qstn = qstn.replace(/\s+/g, " ");
    
    // remove multiple spaces
    qstn = qstn.replace(/[ ]{2,}/gi," ");
    
    // remove leading spaces
    qstn = qstn.replace(/^\s+/, '');
    
    // remove trailing spaces
    qstn = qstn.replace(/\s+$/, '');
    
    return qstn;
}