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

Rob*_*bin 1 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;
}
Run Code Online (Sandbox Code Playgroud)

即使一个案例条件为真,也永远不会进入案例。

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

rad*_*aph 5

replace()如果目标子字符串不存在,则调用不会造成任何危害。因此switch不需要:

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