Reg Expression - Fetch string between 2 characters

Discussion in 'JavaScript' started by lbalance, Aug 26, 2009.

  1. #1
    I am using AJAX to grab the contents of a webpage and I want to grab all instances of a sting pattern and put them into an array.

    The data I am extracting is in Sharepoint. I want all values that are between "-->" and " ".

    I already have the entire data chunk in a variable called "content".

    Can anyone help with a regexp to achieve this?

    Thanks
     
    lbalance, Aug 26, 2009 IP
  2. Logic Ali

    Logic Ali Well-Known Member

    Messages:
    170
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    108
    #2
    <script type="text/javascript">
    
    function extractFromString(str, rxp, index)
    {
     var set = [], result;
    
     while( result = rxp.exec( str ) )
      set.push( result[index] );
    
     return set;   
    }
    
    
    var data = "--> balls &nbsp; xxxx --- ==== --> are &nbsp; never shaped --> round !&nbsp;" ;
    
    alert( extractFromString( data, /-->(.+?)&nbsp;/g, 1 ) );
    
    </script>
    Code (markup):
     
    Logic Ali, Aug 26, 2009 IP
  3. unigogo

    unigogo Peon

    Messages:
    286
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this,

    var data = "--> balls &nbsp; xxxx --- ==== --> are &nbsp; never shaped --> round !&nbsp;" ;
    var myArray = data.split(/-->|&nbsp;/);

    Tested here, &nbsp; displays as a space.
    http://www.pagecolumn.com/tool/regtest.htm
     
    unigogo, Aug 28, 2009 IP