Arunkumar Vasudevan
Arunkumar Vasudevan

Reputation: 5330

Array value needs to Text

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

Answers (3)

Curious
Curious

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

Ja͢ck
Ja͢ck

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

Kiran
Kiran

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

DEMO

Upvotes: 1

Related Questions