用javascript替换ÅÄÖ(大写和小写)

时间:2015-02-16 16:08:22

标签: javascript sharepoint

我正在尝试在用户输入(列名)变量中替换Å,Ä和Ö。这个变量需要ex。 x00e5 用于“å”等使用从SharePoint检索列(通过internalName),因此我需要正确的格式。

我正在检查输入值是否具有Å,Ä和Ö(包括大写和下限)中的任何一个:

switch (inputValue) {
 case inputValue.indexOf('å') > -1:
   inputValue = inputValue.replace(/å/g, '_x00e5_');
   break;
 case inputValue.indexOf('Å') > -1:
   inputValue = inputValue.replace(/Å/g, '_x00c5_');
   break;
 case inputValue.indexOf('ä') > -1:
   inputValue = inputValue.replace(/ä/g, '_x00e4_');
   break;
 case inputValue.indexOf('Ä') > -1:
   inputValue = inputValue.replace(/Ä/g, '_x00c4_');
   break;
 case inputValue.indexOf('ö') > -1:
   inputValue = inputValue.replace(/ö/g, '_x00e6_');
   break;
 case inputValue.indexOf('Ö') > -1:
   inputValue = inputValue.replace(/Ö/g, '_x00c6_');
   break;
 default:
   break;
}

即使一个案例条件成立,它也永远不会进入案件。

这不是最简单/最好的解决方案吗?

1 个答案:

答案 0 :(得分:5)

如果目标子字符串不存在,则调用replace()没有任何害处。因此不需要switch

inputValue = inputValue
  .replace(/å/g, '_x00e5_')
  .replace(/Å/g, '_x00c5_')
  .replace(/ä/g, '_x00e4_')
  .replace(/Ä/g, '_x00c4_')
  .replace(/ö/g, '_x00e6_')
  .replace(/Ö/g, '_x00c6_');