I need to remove a prefix from a string. Given an array of known prefixes, I do not know which prefix will exist in a string. One and only one prefix will exist.
function CleanupSupportedItems(data) {
var prefixes = new Array("TrialLeads", "IPG");
for (var i = 0; i < prefixes.length - 1; i++) {
var prefix = new RegExp(/prefixes[i]/g);
//alert(prefix);
data = data.replace(prefix, "");
alert(data);
}
}
The above code returns undefined on the second iteration.
Given the call
CleanupSupportedItems("TrialLeads11");
I want a return value of “11”. How can I do it?
You can do without the loop:
Here’s demo: http://jsfiddle.net/mrchief/KkRFp/
Updated to make sure it replaced only prefixes and not in between body. Thanks to minitech for pointing it out.
As a suggested improvement, if the input prefixes are expected to have Regex Special characters, they would need to be escaped.