我有这样的数组
在数组中,某些元素包含(3.5-inch) ,(4-inch),(4-inch 64-bit), (4-inch 64-bit),(64-bit)这样的

[
"iPhone - Simulator - iOS 6.1",
"iPhone - Simulator - iOS 7.1",
"iPhone Retina (3.5-inch) - Simulator - iOS 6.1",
"iPhone Retina (3.5-inch) - Simulator - iOS 7.1",
"iPhone Retina (4-inch) - Simulator - iOS 6.1",
"iPhone Retina (4-inch) - Simulator - iOS 7.1",
"iPhone Retina (4-inch 64-bit) - Simulator - iOS 6.1",
"iPhone Retina (4-inch 64-bit) - Simulator - iOS 7.1",
"iPad - Simulator - iOS 6.1","iPad - Simulator - iOS 7.1",
"iPad Retina - Simulator - iOS 6.1","iPad Retina - Simulator - iOS 7.1",
"iPad Retina (64-bit) - Simulator - iOS 6.1",
"iPad Retina (64-bit) - Simulator - iOS 7.1",
""
]


我需要将括号内的-替换为#之类的(3.5#inch) ,(4#inch),(4#inch 64#bit), (4#inch 64#bit),(64#bit)

我怎么可能。

最佳答案

您可以尝试以下方法:

var arr = ["iPhone - Simulator - iOS 6.1", "iPhone - Simulator - iOS 7.1", "iPhone Retina (3.5-inch) - Simulator - iOS 6.1", "iPhone Retina (3.5-inch) - Simulator - iOS 7.1", "iPhone Retina (4-inch) - Simulator - iOS 6.1", "iPhone Retina (4-inch) - Simulator - iOS 7.1", "iPhone Retina (4-inch 64-bit) - Simulator - iOS 6.1", "iPhone Retina (4-inch 64-bit) - Simulator - iOS 7.1", "iPad - Simulator - iOS 6.1", "iPad - Simulator - iOS 7.1", "iPad Retina - Simulator - iOS 6.1", "iPad Retina - Simulator - iOS 7.1", "iPad Retina (64-bit) - Simulator - iOS 6.1", "iPad Retina (64-bit) - Simulator - iOS 7.1", ""];
console.log('before', arr);
var i = arr.length;
while (i--) {
    arr[i] = arr[i].replace(/(-)(inch)/g, '#$2').replace(/(-)(bit)/g, '#$2');
}
console.log('after', arr);

09-25 16:24