2

I am trying to split a string up but am having a problem in doing it.

My string is:

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

I need to be able to Remove all spaces from the string (I am using the following code)

EventList = EventList.replace(/\s/g,'');

I them need to replace all | with , (comma) (I am using the following code)

EventList = EventList.replace('|',',');

I then need to split the string up by using the , (comma) (I am using the following code)

EventList = EventList.split(','); 

I am trying to alart 0x2 from my string (I am using the following code)

alert(EventList[5]);

However, it is alerting 0x2|0x0 as the string and not 0x2.

My full code looks like this:

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

Anyone know where I have gone wrong?

asked Aug 20, 2012 at 1:43
5
  • Have you tried EventList.replace(/ /g, '')? Commented Aug 20, 2012 at 1:45
  • @Jared Farrish - Yes that didn't work. It left the space there but if I used my way of removing the space it works Commented Aug 20, 2012 at 1:47
  • I don't get it: why use a regex to replace a space? Are you expecting other forms of whitespace? Commented Aug 20, 2012 at 1:51
  • @arxanas - I think most of it has to do with the number, otherwise it will only replace just the first instance. I could be wrong, though. Commented Aug 20, 2012 at 2:09
  • (" " + " " + " ").replace(" ", ""); does in fact return "". Commented Aug 20, 2012 at 2:10

3 Answers 3

5

If you use a string as the first argument of .replace(), it will only convert the first ocurrence.

var EventList = "a|b|c|d";
EventList = EventList.replace('|',',');
alert("a,b|c|d"); // displays "a,b|c|d"

You need to use a regular expression with the /g global flag, like you did in the first place.

EventList = EventList.replace(/\|/g,','); // replace any | with ,

(| needs to be escaped with a \ backslash in the regular expression because it has a special meaning in regular expression syntax.)

I made this replacement and it displayed "0x2" as you said it should.

answered Aug 20, 2012 at 1:46
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. This did the job. Knew it would be something simple, just couldn't see it.
0

You need to do a global replace of the pipe bar /|/g. I believe I have encounter this in the past - by default replace in not global in JS.

answered Aug 20, 2012 at 1:46

Comments

0

Small mistake in your second replace. Should use a regex to replace "|". See below:

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(/\|/g,','); // replace any | with ,
EventList = EventList.split(','); // Split EventList
alert(EventList[5]); // alerts 0x2
answered Aug 20, 2012 at 1:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.