Search code examples
javascriptsapui5

Remove spaces from a string, special characters and convert it to lowercase


I need to format a string for comparison purpose. Lets say we have Multiple Choice I want to convert it to multiplechoice So white spaces removed, any special characters removed and lowercase.

I need to do this in SAPUI5 while comparing a value which I get from a model.

if (oCurrentQuestionModel.getProperty("/type") === "multiple choice")

How can I achieve this?


Solution

  • You can do it as:

    var str = "Multiple Choice";
    var strLower = str.toLowerCase();
    
    strLower.replace(/\s/g, '');
    

    Working demo.

    The Regex

    \s is the regex for "whitespace", and g is the "global" flag, meaning match all \s (whitespaces).