我正在尝试拆分字符串,但在执行此操作时遇到问题。
我的字符串是:
var EventList = "0x0,0x1,0x1 | 0x0,0xff,0x2 | 0x0,0x1,0x1 | 0x0,0x1,0x1 | 0x0,0xff,0x5 | 0x0,0xff,0x7 | 0x0,0xff,0x3 | 0x0,0xff,0x6";
我需要能够从字符串中删除所有空格(我正在使用以下代码)
EventList = EventList.replace(/\s/g,'');
我他们需要全部更换| with , (comma) (我使用下面的代码)
EventList = EventList.replace('|',',');
然后我需要使用 , (逗号)拆分字符串(我正在使用以下代码)
EventList = EventList.split(',');
我正在尝试从我的字符串中发出 0x2 警报(我正在使用以下代码)
警报(事件列表 [5]);
但是,它警告 0x2|0x0 作为字符串而不是 0x2。
我的完整代码如下所示:
var EventList = "0x0,0x1,0x1 | 0x0,0xff,0x2 | 0x0,0x1,0x1 | 0x0,0x1,0x1 | 0x0,0xff,0x5 | 0x0,0xff,0x7 | 0x0,0xff,0x3 | 0x0,0xff,0x6";
EventList = EventList.replace(/\s/g,''); // replace any spaces in EventList
EventList = EventList.replace('|',','); // replace any | with ,
EventList = EventList.split(','); // Split EventList
alert(EventList[5]); // should alert 0x2 but it alerts 0x2|0x0
有谁知道我哪里出错了?