最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove spaces from a string, special characters and convert it to lowercase - Stack Overflow

programmeradmin2浏览0评论

I need to format a string for parison 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 paring a value which I get from a model.

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

How can I achieve this?

I need to format a string for parison 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 paring a value which I get from a model.

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

How can I achieve this?

Share Improve this question edited May 16, 2017 at 7:46 loki asked May 16, 2017 at 7:29 lokiloki 6481 gold badge18 silver badges39 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

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).

function cleaner(str) {

    if (str) {

        var strLower = str.toLowerCase();
        return strLower.replace(/\W/g, '');

    }

    return false;

}
发布评论

评论列表(0)

  1. 暂无评论