www.webdeveloper.com
Results 1 to 5 of 5

Thread: Parse Question

  1. #1
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Question Parse Question

    In the following script, can someone explain where the extra ',,,,' are comming from, and better yet, how to get rid of them.

    For example: 123+456 gives an alert message of ,123,,,,+,,456,,
    What I would like to see is: 123,+,456

    PHP Code:
    <html>
    <
    head>
    <
    title>Parse</title>
    <
    script type="text/javascript">
      function 
    strToTokens(str) {
        
    str str.replace(/(\s+)/g,' ');
        var 
    tokens = new Array();
        var 
    tokens_index 0;
        
    tokens str.split(/(\w+)|(\W+)/i);
        
    alert('StrToTokens:'+tokens);        
      }
    </script>  
    </head>
    <body>
    Parse Test<br>
    <input type="text" id="StrInfo" value="">
    <button onClick="strToTokens(document.getElementById('StrInfo').value)">Action</button>
    <input name="tst" type="radio" value="123+456" onClick="document.getElementById('StrInfo').value=this.value">
    <input name="tst" type="radio" value="a=12+(-34)+56" onClick="document.getElementById('StrInfo').value=this.value">
    <input name="tst" type="radio" value="c2=a^2+b^2" onClick="document.getElementById('StrInfo').value=this.value">
    </body>
    </html> 
    The extra radiobuttons are just for testing setup purposes.

    Ideally I would like to split a string into Numbers, Variables (letters & numbers) and other characters like *,/,!, etc. to use in an Infix to Postfix (RPN) conversion script.

    Any help is appreciated.

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    str.split(/(w+)|(W+)/i);
    If you are splitting the input string on either lowercase w's or uppercase W's,
    you don't need the i flag. Those are the only two cases w has.

    But I don't see where all those ws are coming from.
    And why did you replace sequences of the letter s with a space?
    str = str.replace(/(s+)/g,' ');
    And what is poor tokens_index doing?
    Last edited by mrhoo; 02-28-2007 at 10:49 PM.

  3. #3
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Thanks for the reply "mrhoo"

    If you are splitting the input string on either lowercase w's or uppercase W's,
    you don't need the i flag. Those are the only two cases w has.
    I'll get rid if the i flag. It was left over from a previous attempt when I was using something like /(\s+)|(\d+)/i and /ig test expressions...

    But I don't see where all those ws are coming from.
    When I used only the \w or only the \W, I got only numbers and variables with letters. It left out the other characters in the string.

    And why did you replace sequences of the letter s with a space?
    This was to remove any extra spaces between the "tokens" so they would not show up as blank tokens. In an earlier attempt I had used \s as a split value and was getting incorrect results with extra spaces in the entries.

    And what is poor tokens_index doing?
    You're right, it is not needed. Again, it was left over from a previous attempt before I tried getting help from this forum.

    Thanks for your response ... any other ideas? I'm definitely not an expert on regular expression (regex) formations. It just looked like a good idea at the time.

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Bump.

    Any ideas from the mid-day crowd?
    All my late-night lookers seem to have gone to sleep!

  5. #5
    Join Date
    Feb 2006
    Posts
    2,927
    Try matching:

    Code:
    function strToTokens(str){
    	var tokens= str.match(/(\w+)|(\W+)/g)
    	alert('StrToTokens:'+tokens.join(','));
    }
    strToTokens('124+45-23'); // returns>> '124,+, 45,-, 23'

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles