0

I don't have previous experience working with regex and that's why I need your help.

I have 3 different web services which I'm using to grab data via live search. Here is the logic of my statement which I'm trying to implement.

if(carName is entered) { getCarName.php?text=blablabla } elseif (carModel is entered) { getCarModel.php?text=blablabla } elseif (carNumer is entered) { getCarNumber.php?text=1337 } 

The idea is if carName is entered it could be a simple string like Pontiac Firebird 2002, then carModel can be OHC inline-6 and a single-barrel carburetor. carName and carModel may contain characters and numbers. carNumber can have numbers-only value like 194378.

I've started evaluation regexr.com website, but still didn't came up with a possible solution to my problem.

I want to have 1 input field (search field). I have different 3 web services. I have a working live search for 1 web service (carName). It's working fine, but I want to make a regex statement to find out when in the input search field is entered carName or carModel or even carNumber. How I can do it?

As an example, here is a code snippet for my live search for carName web service:

$('#search').keyup(function() { var searchField = $('#search').val(); var $update = $('#update'); $.get("getCarName.php?text=" + searchField, function(data) { $update.empty(); var vals = jQuery.parseJSON(data); if($.isArray(vals['Car'])) { $.each(vals['car'], function(k,v){ $update.append("<li value='"+v['id']+"'><a href='#'>" + v['car_name'] + "</a></li>"); }); } else { $update.append("<li value='"+vals['car']['id']+"'><a href='#'>" + vals['car']['car_name'] + "</a></li>"); } }); }); 

Based on a code above, I want to create an if/else statement to figure it what which web service it should use based on the input string.

According to the regexr.com carName and carModel can be achieved from:

abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789 

And carNumber from:

0123456789 

Buy how I can create a If/Else condition according to this?

Can please anyone help me with that?

5
  • <input type="number" ...> :) Commented Nov 25, 2014 at 11:13
  • Please describe concisely what you're trying to do. Your pseudo-code is IMO incomprehensible. Commented Nov 25, 2014 at 11:14
  • It's unclear what the problem is or why you'd even need a regular expression for this. Commented Nov 25, 2014 at 11:15
  • It's relatively easy to detect the car number if it's only numbers, but what's the difference between car name and car model? They both seem to contain both letters and numbers. Commented Nov 25, 2014 at 11:29
  • Yep, updated a question a bit. How I can create a if/else condition at least based on carNumber? Commented Nov 25, 2014 at 11:33

1 Answer 1

1

Possible suggestion:

1) You can track if input string contains only numbers (means carNumber is entered) by only this regex '[\d]+'.

2) If input contains not only digits, you can make an array or smth containing all possible car manufacturers names and looking if some word in your input string matches it. If matches, then search by CarName

3) If none of above applied - CarModel is entered

This is somehow working example

var string = 'your input 2014'; var regex = /[\D]+/; var carsArray = ["Nissan", "Pontiac", "Other one"]; function findInArray(a, s) { for( var i = 0; i < a.length; ++i ) if( a[i].indexOf( s ) >= 0 ) return i; return -1; } if (regex.exec(string)) { //there are NOT only numbers if (findInArray(carsArray, string) == '-1') { //carModel entered! }else{ //carName entered! } } else { //carNumber entered! } 
Sign up to request clarification or add additional context in comments.

1 Comment

Can you please provide a conditional if/else statement, because I'm looking for the help with it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.