Regex   sum  (  itemOne  *   itemTwo +  itemTwoX -  itemThree ..... )

Posted on 2005-04-26
Last Modified: 2006-11-18
I need to extract these items : itemOne,itemTwo, etc. Basically, every word between the parenthesis. There may be spaces before/after ( ) and the seperators might be [+/*-] .

I have tried this, but the parenthesis seem to be messing up the expression (combined with my newbieness on regex I'm sure) .

FYI: This is going into a web page. The itemOne,itemTwo,etc are elements that I need to put onchange events into so that if any element in the expression changes, the calculation is done automatically. I wouldn't know the fields during the design of the page.

Thanks Experts !
Question by:lrr81765
    LVL 32

    Accepted Solution


    <script type="text/javascript">
    var string = "sum  (  itemOne  *   itemTwo +  itemTwoX -  itemThree )";
    var items = string.split(/[\*\+\-\/]/g);

    for(var no=0;no<items.length;no++){
        items[no] = items[no].replace(/.*?\((.*?)/,'$1').replace(/\)/g,'');
        items[no] = items[no].replace(/\s*?([^\s]+)\s*/,'$1');


    Author Comment

    Any chance you might have a .net RegEx example of this same thing? That is where the actual processing takes place (sorry for that).
    LVL 32

    Expert Comment

    Sorry, my .net knowledge is extremely limited. If .net supports standard regexp, then it should work if you find .net's functions for split and replace.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now