2013-06-18 31 views
0

我想写一个JavaScript函数,将采取字符串<0>(其中0真的可以是任意数字),在它和删除它,得到它的数量的实际值。例如,如果它被赋予了这样一句话:如何获得Javascript正则表达式匹配的字符串的实际值?

'By now you\'re probably familiar with <0>X, a drawing application that has won an <1>Awards and plenty of attention for its user interface, which has rethought the way basic interactions like <2>pinch-to-zoom or <3>color selection should work on a touchscreen.' 

我希望它给我回这个字符串:

'By now you\'re probably familiar with X, a drawing application that has won an Awards and plenty of attention for its user interface, which has rethought the way basic interactions like pinch-to-zoom or color selection should work on a touchscreen.' 

这阵:

[0, 1, 2, 3] 

目前我有这样的:

function(sentence) { 
    return sentence.split(/\<[0-9]+\>/).join(''); 
} 

显然刚刚返回这句话。我需要在标签内有数字值。有没有办法做到这一点?

+2

是否需要纠正奖的拼写神奇吗? – Rudu

+0

你希望它去掉'<0>'字符,并返回一个数组*和*这些数字被剥离的句子? –

+0

@Rudu哎呀。大声笑没有 – chromedude

回答

2

我建议:

function regexAndArray (str) { 
    var reg = /(<(\d+)>)/g, 
     results = { 
      string : '', 
      stripped : [] 
     }; 
    results.string = str.replace(reg, function(a,b,c){ 
     results.stripped.push(c); 
     return ''; 
    }); 
    return results; 
} 

console.log(regexAndArray('By now you\'re probably familiar with <0>X, a drawing application that has won an <1>Awards and plenty of attention for its user interface, which has rethought the way basic interactions like <2>pinch-to-zoom or <3>color selection should work on a touchscreen.')); 

JS Fiddle demo

参考文献:

+0

太棒了。谢谢! – chromedude

+0

你真的很受欢迎,很高兴得到了帮助。 –

相关问题