views:

63

answers:

1

i have a table with code like this:

 <tr>
               <th scope="row">5-17</th>
               <td>23.6 (22.0-24.0)</td>
               <td>18.0 (17-20.0)</td>
           </tr>
           <tr>
               <th scope="row">Total 0-17</th>
               <td>20.6 (8-15.6)</td>
               <td>16.1 (22.2-24.0)</td>
           </tr>

i am using the following function to find the contents of the table cells and bring it into an array:

var l1 =   $('#datatable td:nth-child(2)').map(function() {
     return $(this).text();
    }).get();

the table fragment's id is "datatable".

this returns properly, however i am putting this array into jqPlot and it cant read the stuff between the parentheses or the parentheses themselves.

i need the data to remain in the table however because we are displaying both the table and the chart.

i cant reformat (add spans around the content i want to remove etc) because these tables are being generated by another piece of software.

i think what i need to do is either slice off after the space or use a regex to find and remove. not sure how to proceed with either of these. thanks!

A: 

You can use:

return $(this).text().match(/\S+/)[0];

This will take only everything up to the first space.

In a regular expression:

\S means a non-whitespace character.
\S+ means a sequence of one or more non-whitespace characters.

The regex will take as many non-whitespace characters as possible and return them.

The [0] at the end is needed because the match function returns an array, in this case consisting of only one element.

Edit: fixed bug in above code: it was missing the [0]

interjay
THANK YOU! i knew there had to be something like that...i just didnt know regex well enough.
liz