Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Reading specific values from string:

Last post 08-10-2008, 7:06 PM by mocambo. 3 replies.
Sort Posts: Previous Next
  •  08-09-2008, 8:03 PM 45168

    Reading specific values from string:

    I'm trying to read some specific values from a string.

    elementName = "item add item:mk0090 destination:project1";

     You can see there two  key:value  pairs. How can I find this value knowing an key ?

    Matching 'item:mk0090' or 'destination:project1' is easy:

    elementID.match( '/\b' + 'item' + ':[A-Z0-9]\b/', elementName);

    ==> item:mk0090

    but how to capture for corresponding item value or destination value with regular expression which works also with IE6 (starting at Javascript 1.3) ?

    Please help !

  •  08-10-2008, 7:01 AM 45170 in reply to 45168

    Re: Reading specific values from string:

    Please be more specific about your goal, if it is to reference the matched colon-separated values in that string:

    Raw Match Pattern:
    ([^ ]*):([^ ]*)

    
    Javascript Code Example:
    <script type="text/javascript">
      var re = /([^ ]*):([^ ]*)/g;
      var sourcestring = "source string to match with pattern";
      var results = new Array();
      var ii = 0;
      for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
        results[ii] = new Array();
        for (var j=0; j<matches.length; j++) {
          results[ii][j]=matches[j];
          alert("results["+ii+"]["+j+"] = " + results[ii][j]);
        }
        ii++;
      }
    </script>
    


    $matches Array:
    (
        [0] => Array
            (
                [0] => item:mk0090
                [1] => destination:project1
            )

        [1] => Array
            (
                [0] => item
                [1] => destination
            )

        [2] => Array
            (
                [0] => mk0090
                [1] => project1
            )

    )


  •  08-10-2008, 6:32 PM 45172 in reply to 45170

    Re: Reading specific values from string:

    Thank you for your assistance !

    More specific goal is find VALUE knowing a KEY from STRING.

    sampleString =  "source_string_to_KEY1:VALUE1_match_with_pattern_KEY2:VALUE2"

    So for example I need to capture (only) VALUE1 when i know only KEY1 from that sampleString.

    Key:Value pairs are with colon-separated form. All words are underline-separated in that string. String is used in DOM Element ID name.

    Yes, I can solve this with unskilful string handling. Like splitting string by '_' to substrings, finding stings, that contains ':', then comparing first part of this substring with knowing key and finally return last part as a match.

    Probably there is possibility find this match simply with regular expression. This regex should find this key:value pair but only capture required value part. Is there any possibility ?

    This is my goal.
     

  •  08-10-2008, 7:06 PM 45173 in reply to 45170

    Re: Reading specific values from string:

    I used now your excellent tester at myregextester.com and my best result is:

    Source text:
    source_string_to_KEY1:VALUE1_match_with_pattern_KEY2:VALUE2 
    Raw Match Pattern:
    (?=KEY1:([^_]*))
    $matches Array:
    (
    [0] =>
    [1] => VALUE1
    )
    This solution does'nt work with IE6 (below JS 1.5) 
    But I'm looking for something like:
    $matches Array:
    (
    [0] => VALUE1
    )
     

    Which works with IE6 (JS 1.3)

View as RSS news feed in XML