0

I have this formula in a javascript string:

var testString = '(var01+var02+var03)/3';

I would like to know which is the best way to obtain the following result in a array:

['var01', 'var02', 'var03']
4
  • 3
    Are they meant to be variables or strings in that array? Commented Apr 9, 2015 at 11:21
  • Your formula won't change ever ? Commented Apr 9, 2015 at 11:24
  • Do you wand to extract only variable names from your string? Commented Apr 9, 2015 at 11:24
  • They are meant to be strings in that array. Commented Apr 9, 2015 at 11:25

1 Answer 1

2

Use a regular expression to match word characters:

var testString = '(var01+var02+var03)/3';
var result = testString.match(/[a-z]\w*/gi);
alert(JSON.stringify(result));

The g modifier makes it return an array of all matches.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.