简体   繁体   English

删除字符串中的空格,特殊字符并将其转换为小写

[英]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. 可以说我们有“ 多项选择”,我想将其转换为“ multiplechoice”。因此,删除了空格,删除了所有特殊字符,并将其转换为小写。

I need to do this in SAPUI5 while comparing a value which I get from a model. 在比较从模型获得的值时,我需要在SAPUI5中执行此操作。

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

How can I achieve this? 我该如何实现?

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). \\s是“空白”的正则表达式,而g是“全局”标志,表示匹配所有\\s (空白)。

function cleaner(str) {

    if (str) {

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

    }

    return false;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM