我有这样的数组
在数组中,一些元素包含(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)
我怎么可能。
答案 0 :(得分:1)
你可以试试这个:
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);
答案 1 :(得分:1)
这适用于括号内-
的所有可能出现的内容:
var data = [
"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",
""
];
data = data.map(function(value) {
// first find the brackets
return value.replace(/\([^)]*\)/g, function(val) {
// now replace all occurences of - with #
return val.replace(/-/g, "#");
});
});
以下是 jsfiddle-example
答案 2 :(得分:0)
var list = [
"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",
""
]
var new_list = list.map(function(item){
return item.replace(/\(.*?\)/g, function(m){
return m.replace(/-/g, "#")
});
});
答案 3 :(得分:0)
您可以使用array.map(),因为:
var replaced = your_arr.map(function(val) {
return val.replace(/(-)(inch|bit)/, "#$2");
});
console.log(replaced);