-1

私は急いでいます。解決する必要があるのは以下のとおりです

DropDown1_2_3_4 または radioButton2_4_6_8 のような文字列があります

次のような結果を取得する必要があります: 1,2,3,4 または 2,4,6,8

var id = $(this).attr('id');
var main = id;
main = main.substring(main.indexOf('_') + 1).replace(/[^\d]/g, '');

これにより、234 または 468 が得られます。必要なのは、1,2,3,4 または 2,4,6,8 です。

よろしく

4

3 に答える 3

0

先読みアサーションの使用:

'DropDown1_2_3_4'.match(/\d+(?=_)|_\d+/g).map(function(x) { return x.replace('_', ''); })
// => ["1", "2", "3", "4"]
'radioButton2_4_6_8'.match(/\d+(?=_)|_\d+/g).map(function(x) { return x.replace('_', ''); })
// => ["2", "4", "6", "8"]

var id = $(this).attr('id');
var main = id.match(/\d+(?=_)|_\d+/g).map(function(x) { return x.replace('_', ''); });

Javascript 正規表現は後読みアサーションをサポートしていません。

于 2013-11-02T06:06:38.487 に答える