0

我正在尝试编写一个 Javascript 函数,该函数将在其中获取一个字符串<0>(其中 0 实际上可以是任何数字)并将其删除并获取其中数字的实际值。例如,如果给出这句话:

'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('');
}

这显然只是返回句子。我需要在标签内包含数字值。有没有办法做到这一点?

4

1 回答 1

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 小提琴演示

参考:

于 2013-06-18T16:21:06.143 回答