Alex
Alex

Reputation: 67244

Remove prefix from a list of strings

How can I remove a sub-string (a prefix) from a array of string elements? (remove the sub string from each element)

Upvotes: 45

Views: 47158

Answers (10)

AJcodez
AJcodez

Reputation: 34224

function trimPrefix(str, prefix) {
    if (str.startsWith(prefix)) {
        return str.slice(prefix.length)
    } else {
        return str
    }
}

var prefix = "DynamicPrefix"
trimPrefix("DynamicPrefix other content", prefix)

Upvotes: 22

Jake Feasel
Jake Feasel

Reputation: 16945

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.

Upvotes: 2

user1046495
user1046495

Reputation:

var pre = 'prefix_';

my_arr = my_arr.map(function(v){ return v.slice(pre.length); });

See MDN if full browser support for .map() is needed.

you can also use .forEach() if you need to keep the original array.

var pre = 'prefix_';

my_arr.forEach(function(v,i){ my_arr[i] = v.slice(pre.length); });

Upvotes: 8

Joe
Joe

Reputation: 82654

Using RegExp and ^ to ensure it is the prefix and not just somewhere in the string:

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

Upvotes: 50

Wayne
Wayne

Reputation: 60424

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), '');

Upvotes: 15

Paul
Paul

Reputation: 141897

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

Upvotes: 4

bozdoz
bozdoz

Reputation: 12880

I would do this:

var element = document.getElementsByTagName('p');
for(i in element){
 str = element[i].innerHTML;
 str = str.replace(/^pre_/,'');
 element[i].innerHTML = str;
}

Upvotes: 1

ipr101
ipr101

Reputation: 24236

You could use the jQuery map function -

var arr = $.map(['a1', 'a2'],function (s) {
    return s.replace(/^a/,'');
});

Upvotes: 1

Alex Turpin
Alex Turpin

Reputation: 47776

Simply loop through them?

var list = ["foo", "bar", "meh"];
for(var i = 0; i < list.length; i++)
    list[i] = list[i].substr(1, 1);

http://jsfiddle.net/DcvE2/1/

Upvotes: 1

talnicolas
talnicolas

Reputation: 14053

Just iterate on your list of string (with a for loop for example) and use the replace method (details here)

Upvotes: 1

Related Questions