Javascript 从字符串列表中删除前缀
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8128894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove prefix from a list of strings
提问by Alex
How can I remove a sub-string (a prefix) from a array of string elements? (remove the sub string from each element)
如何从字符串元素数组中删除子字符串(前缀)?(从每个元素中删除子字符串)
回答by Joe
Using RegExp
and ^
to ensure it is the prefix and not just somewhere in the string:
使用RegExp
and^
确保它是前缀,而不仅仅是字符串中的某个地方:
var arr = ['a1', 'a2', 'a54a'];
for(var i = 0, len = arr.length; i < len; i++) {
arr[i] = arr[i].replace(/^a/, '');
}
arr; // '1,2,54a' removing the 'a' at the begining
回答by AJcodez
function trimPrefix(str, prefix) {
if (str.startsWith(prefix)) {
return str.slice(prefix.length)
} else {
return str
}
}
var prefix = "DynamicPrefix"
trimPrefix("DynamicPrefix other content", prefix)
回答by Wayne
Many of the answers already given are wrong, because they'll remove the target string from anywhere in each of the elements (not just the beginning). Here's another approach:
已经给出的许多答案都是错误的,因为它们会从每个元素的任何位置(不仅仅是开头)删除目标字符串。这是另一种方法:
var str = "str_";
["str_one", "str_two_str_", "str_three"].map(function(el) {
return el.replace(new RegExp("^" + str), '');
});
Result:
结果:
["one", "two_str_", "three"]
Or, if you prefer simple iteration (with no higher-order function):
或者,如果您更喜欢简单迭代(没有高阶函数):
var str = "str_";
var list = ["str_one", "str_two_str_", "str_three"];
for (var i = 0; i < list.length; i++)
list[i] = list[i].replace(new RegExp("^" + str), '');
回答by Wayne
var pre = 'prefix_';
my_arr = my_arr.map(function(v){ return v.slice(pre.length); });
See MDNif full browser support for .map()
is needed.
如果需要完整的浏览器支持,请参阅 MDN.map()
。
you can also use .forEach()
if you need to keep the original array.
.forEach()
如果需要保留原始数组,也可以使用。
var pre = 'prefix_';
my_arr.forEach(function(v,i){ my_arr[i] = v.slice(pre.length); });
回答by Paul
var i;
for(i = 0; i < arr.length; i++)
arr[i] = arr[i].replace(substr, '');
Example:
例子:
var arr = ['test1', '2test', '3test3'];
// Use only one of these lines
var substr = 'test'; // This is for substrings
var substr = /^test/; // This is for prefixes only
var i;
for(i = 0; i < arr.length; i++)
arr[i] = arr[i].replace(substr, '');
console.log(arr); // Prints ["1", "2", "33"] to console
回答by Jake Feasel
Just for some variety:
只是为了一些品种:
substr = new RegExp('(^|\|)prefix_', 'g');
arr = arr.join('|').replace(substr, '').split('|')
edit- to show how you can limit to just the prefix with the right regexp.
编辑- 显示如何使用正确的正则表达式限制为前缀。
回答by ipr101
You could use the jQuery map
function -
你可以使用 jQuerymap
函数 -
var arr = $.map(['a1', 'a2'],function (s) {
return s.replace(/^a/,'');
});
回答by bozdoz
回答by talnicolas
Just iterate on your list of string (with a for loop for example) and use the replace
method (details here)
只需迭代您的字符串列表(例如使用 for 循环)并使用该replace
方法(详情请点击此处)
回答by Alex Turpin
Simply loop through them?
简单地遍历它们?
var list = ["foo", "bar", "meh"];
for(var i = 0; i < list.length; i++)
list[i] = list[i].substr(1, 1);