Reputation: 5330
I have a Array like
text=['2','2<sup>2</sup>','3<sup>10</sup>'.......];
I want result like this
text=['2','22','310'......];
How can i get This using javascript
var optionTxt = (xmlDoc.getElementsByTagName('Option')[i].textContent ? xmlDoc.getElementsByTagName('Option')[i].textContent : xmlDoc.getElementsByTagName('Option')[i].text);
optionList[i] = $.trim(optionTxt);
Upvotes: 3
Views: 75
Reputation: 11
Try jQuery html parsing:
var text = ['2', '2<sup>2</sup>', '3<sup>10</sup>'];
var out = [];
jQuery.each(text, function (si, str) {
var concat = '';
jQuery.each(jQuery.parseHTML(str), function (ei, el) {
concat = concat + el.textContent;
});
out.push(concat);
});
check out this fiddle: http://jsfiddle.net/9cV7M/
Upvotes: 0
Reputation: 173572
You can use a .map
operation for that and replace any non-digit with nothing using .replace()
:
text.map(function(item) {
return item.replace(/\D/g, '');
});
Since you're using jQuery you might also use their .map
instead to fully benefit from cross (old) browser compatibility:
$.map(text, function(item) {
return item.replace(/\D/g, '');
});
Upvotes: 3
Reputation: 20313
Use .map()
and .replace()
. Try this:
var text=['2','2<sup>2</sup>','3<sup>10</sup>'];
text = $.map(text,function(i){
return i.replace( /[^\d.]/g,'');
});
console.log(text);
Upvotes: 1