I have a table that is displayed using datatables, above each column I have an empty text form field that users can type in terms to filter on. This works fine on all text fields, and works ok on integer fields as well. I am doing some conversion for some terms such as if the user types in NULL or NOT NULL for example I convert that to the regex ^$ or .
I know regex is intended to search text strings but this is what datatables uses so thats why I am doing this. What I want is for users to be able to type in a value such as "x to y" and to be able to convert that to a regular expression. I cannot find a function that does this, if anyone knows of one please let me know.
Assuming a function doesn't already exist, assume that only positive integers will be searched, and say up to 7 digits. so 0 - 9,999,999 can be searched. Also the only way this is triggered is by the keyword to with spaces " to ".
so something like this to start:
function convertNumRangeRegex(s){
if(s.indexOf(" to ") != -1){
var range = s.split(" to ");
lowRange = Number(range[0]);
highRange = Number(range[1]);
if(lowRange >= 0 && lowRange < 10 && highRange < 10){
s = "^[" + lowRange + "-" + highRange + "]$";
}};
return s;
};
This works with numbers 0-9, but expanding on this seems like it would get pretty ugly. I am up for any ides. Thanks.
Forward
3 years later I rediscovered this question, and had some time to solve the puzzle. I'm not entirely clear on why you'd want to use a regex, but I'm sure it has to do with improving database return performance by not forcing all possible results to the client where they'll be evaluated.
That said, I'm sure you have your reasons.
Explanation
This set of functions will construct a regular expression that will do the following:
General overview
The function
funRegexRange
does all the heavy lifting. By building a string that will match all numbers from0
toUpperRange
The function
funBuildRegexForRange
then constructs the actual regex with a negative lookahead and a positive lookahead.The resulting regex will then validate your number is between
0
and theUpperRange
inclusive, and not between0
and theLowerRange
not inclusive.The functions will allow either numbers or strings values, but does not validate the inputs are integers. Providing values which do not equate to integers will yield unpredictable results.
Examples
To get the regex for a range from 400 to 500:
By setting the last parameter to true you it'll show the various parts being constructed and the full regex.
The resulting regex looks like
Asking for a range between 400 - 999999999999 [twelve digits] returns this monster:
Full Regex = /^(?!(?:[0-3][0-9]{2}|[0-9]{1,2})$)(?=(?:[0-8][0-9]{11}|9[0-8][0-9]{10}|99[0-8][0-9]{9}|999[0-8][0-9]{8}|9999[0-8][0-9]{7}|99999[0-8][0-9]{6}|999999[0-8][0-9]{5}|9999999[0-8][0-9]{4}|99999999[0-8][0-9]{3}|999999999[0-8][0-9]{2}|9999999999[0-8][0-9]|99999999999[0-8]|999999999999|[0-9]{1,11})$)/
Javascript Code
Live Example: https://repl.it/CLd4/4
Full code:
Validating a number is in a range of numbers with regex is tricky problem. These regexs will match a number within a given range:
It starts to get out of hand when you have complex ranges
\b(?:5(?:4(?:3(?:2[1-9]|[3-9][0-9])|[4-9][0-9]{2})|[5-9][0-9]{3})|[6-9][0-9]{4}|[1-9][0-9]{5}|[1-8][0-9]{6}|9(?:[0-7][0-9]{5}|8(?:[0-6][0-9]{4}|7(?:[0-5][0-9]{3}|6(?:[0-4][0-9]{2}|5(?:[0-3][0-9]|4[0-3]))))))\b # 54321-9876543