regex help needed

regex help needed

stevieteestevietee Posts: 2Questions: 0Answers: 0
edited February 2011 in General
Hi, something of a noob when it comes to javascript and regex so please bear with with me :).

I have a table here http://www.xperiaplaysmartphone.co.uk, which is basically a comparison of the various deals currently available to the UK for the forthcoming handset.

I am trying to implement some checkbox filtering to allow the user to view all the offers for a combination of networks. This works fine (based on this thread: http://datatables.net/forums/comments.php?DiscussionID=3228). But I have an extra requirement to allow the user to filter on networks and an 18month contract. Problem is I cant seem to construct a regex that works for me.

For example i want to show all the 18 month contracts on Vodafone and O2. I am using

oTable.fnFilter( reg_exp,1, true, false );

and you can see the reg_exp I am building when you click the relevant checkboxs. However as soon as I add 18 months to the mix the filter returns nothing. I think it may just be the way the regex is being built, but I would appreciate any assistance in resolving this.

TIA
Ste

Replies

  • pmccannpmccann Posts: 9Questions: 0Answers: 0
    Hi,

    the pipe symbol "|" means "or", so when clicking on Vodafone or O2 you will want to join them with that symbol. But the (18) you need to trap 18 month contracts *must* be present, so something like

    (Voda|O2).*\(18\)

    should find the required elements. Note that unescaped brackets are a grouping tool, so if you want to match literal brackets, as per the (18), you need to escape the bracket character with a backspace. The .* just means "zero or more of anything".

    More generally, a regexp needs to match *completely* from left to right, so if you want to match two or more elements of your string you'll need to "knit" those elements together somehow, as with the .* above.

    The list of tickboxes across the top is a bit of a problem, because you'll want some of those options to be added with an "or" (as with two different companies), and others to be added with an and (choose a company and a time period). Might need a bit of a rejig...

    Good luck!
    Paul
  • stevieteestevietee Posts: 2Questions: 0Answers: 0
    Excellent, thanks Paul, thats exactly what I was looking for:-)
This discussion has been closed.