Search code examples
jquerygreasemonkeyhighrise

HighRise jQuery parsing


I want to add h323:number style links to HighRise contact numbers, so that users can click a link to dial the IP phone...

The html I'm looking at is:

<table>
  <tbody>
    <tr>
      <th>Phone</th>
      <td>+44 (0)1123 1231312<span>Work</span></td>
    </tr>
    <tr>
      <th></th>
    <td>+44 (0)777 2342342<span>Other</span></td>
    </tr>
  </tbody>
</table>

and basically I want to pull out the number which is in a td and which starts with +44, strip out the spaces and stick in a link inside the span that has an href like

h323:4411231231312  

i.e. is stripping out the 0 in brackets.

Any help would be greatfully received to any of the following.

(1) How do I match the td containing +\d\d numbers? (2) How do I use selectors to exclude the span when I get the number from the td (3) What regex should I use to cleanup the number for the href?


Solution

  • This should be close to what you need.

    $('tbody td').each(function() {
        // match a sequence of digits, parentheses and spaces
        var matches = $(this).text().match(/[ \d()]+/);
    
        if (matches) {
            // remove the spaces and stuff between parentheses
            var href = 'h323:' + matches[0].replace(/\s|\(.*?\)/g, '');
            var link = $('<a/>').attr('href', href);
    
            $('span', this).append(link);
        }
    });
    

    One word of caution though, if a span's content starts with a digit it will be included in the match; is this a possibility that needs to be accounted for?