0

I'm trying to dynamically create an array from another one in JavaScript. I have a string which is a mathematical literal expression like this '2a + 3b + 4a + 5c': I just want to split it into an array with only the literal part of the number (Ex. 'a,b,a,c').

I've tried to use the following code to do this :

var expression = '2a + 3b + 4a + 5c';
var NumbersArray = expression.split(' + '); /* NumbersArray = 2a,3b,4a,5c */
alert('So far it's working!');
var LettersArray = new Array();
for (var i = 0; i < NumbersArray.length; i++) {
    eval('var LettersArray[' + i + '] = NumbersArray[' + i + '].replace(/[0-9]/g,"");');
    alert(eval('LettersArray[' + i + ']'));
}

But it doesn't work! How can I fix it?

5
  • 2
    You've got a syntax error in your first alert, the apostrophe needs to be escaped. Commented Aug 23, 2013 at 16:48
  • Is it copy-paste error, or are the quotes in your alert actually like that? Because that will cause a syntax error (the apostrophe of it's is ending the string). Also, why are you using eval? Commented Aug 23, 2013 at 16:48
  • SO code highlighter already provided an answer. Commented Aug 23, 2013 at 16:48
  • It was a copy-paste error. In my version the alert's text was not "So far it's working!". Commented Aug 23, 2013 at 17:36
  • Wow! Thank you all! I couldn't imagine the power of regular expressions! I' will study them very carefully! Thanks again! Commented Aug 23, 2013 at 17:38

5 Answers 5

5

Lots of errors here, tried to comment all of my fixes:

var expression = '2a + 3b + 4a + 5c';
var NumbersArray = expression.split(' + '); /* NumbersArray = 2a,3b,4a,5c */

//using single quotes inside single quotes doesn't work without escaping, try double quotes instead
alert("So far it's working!");

//use array literals [], not new Array() unless you need it
var LettersArray = [];

for (var i = 0; i < NumbersArray.length; i++) {
    //why is eval used here? and why are you using var to re-declare LettersArray?
    //eval('var LettersArray[' + i + '] = NumbersArray[' + i + '].replace(/[0-9]/g,"");');
    //alert(eval('LettersArray[' + i + ']'));

    //no need for eval, just access array indexes
    LettersArray[i] = NumbersArray[i].replace(/[0-9]/g, '');
    alert(LettersArray[i]);
}

Working example: http://jsfiddle.net/FERj5/

A quicker way to accomplish the same thing would be:

var expression = '2a + 3b + 4a + 5c';
var letters = expression.replace(/\d+/g, '').split(' + ');
//now letters == ['a', 'b', 'a', 'c']
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Thank you all! I couldn't imagine the power of regular expressions! I' will study them very carefully! Thanks again!
1

If it is only going to an array from '2a + 3b + 4a + 5c' to 'a, b, a, c' you can make it a lot shorter:

var expression = '2a + 3b + 4a + 5c';
expression.replace(/(\d+|\s+)/gi,'').split('+');

Comments

1

the string in your alert breaks. It should be

 alert("So far it's working!");

also at eval in your loop same issue is there

this code works

var expression = '2a + 3b + 4a + 5c';
var NumbersArray = expression.split(' + '); /* NumbersArray = 2a,3b,4a,5c */
var LettersArray = [];
var evalit;

for (var i = 0; i < NumbersArray.length; i++) {

    evalit = "LettersArray[" + i + "] = NumbersArray[" + i + "].replace(/[0-9]/g,'');";

    eval(evalit);
    alert(LettersArray[i]);
}

here is a demo fiddle

also try to avoid eval at places where you dont need it.

Comments

0

Not sure why you are using eval. Try this:

var expression = '2a + 3b + 4a + 5c';
var NumbersArray = expression.split(' + '); /* NumbersArray = 2a,3b,4a,5c */
var LettersArray = new Array();

for (var i = 0; i < NumbersArray.length; i++) {

    LettersArray[i] = NumbersArray[i].replace(/[0-9]/g,"");;
    alert(LettersArray[i]);
}

Comments

0

How flexible does this need to be? If your pattern is always going to be [number][letter] + [number][letter] + [number][letter] + [number][letter] + ...., then you could go really basic:

var expression = '2a + 3b + 4a + 5c';
var NumbersArray = expression.split(' + '); /* NumbersArray = 2a,3b,4a,5c */
var LettersArray = expression.match(/[a-z]/g); /* LettersArray = a,b,a,c */

For a little more flexibility, all you would need to do is change up the regex pattern a little bit:

  • If you needed to account for multi-letter variables, you could change /[a-z]/g to /[a-z]+/g.
  • If you needed to account for uppercase variables, you could change /[a-z]/g to /[a-z]+/gi.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.