2

Have a string like this: 'XXX-XXX-XXX'; This can be changed by the user. Than I have another string like this, that uses characters in this string to generate a serial number: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; This can be changed by the user.

Now, I need to loop through a bunch of input values to determine if they match the formula (will always be only X's and dashes. Note: dashes might not even exist in formula, this is user defined and can change from within a different input field altogether).

Basically, there is another group of input fields:

<div class="check_these">
    <input type="text" class="p-serials" name="pserials[]" value="YER-CGH-JOP" />
    <input type="text" class="p-serials" name="pserials[]" value="BAB-CC1-D80" />
    <input type="text" class="p-serials" name="pserials[]" value="JUQ-P" />
    <input type="text" class="p-serials" name="pserials[]" value="XXX-XXX-XXX" />
</div>

So, we have the var formula = 'XXX-XXX-XXX'; variable and the possible characters that are allowed in the string var chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

So, how to change this code below so that it returns only those that match:

var currMatches = $(".p-serials").map(function () {
    // Need to match only formula numbers in here!
    return $(this).val();
}).get();

So, according to the inputs above, currMatches should be equal to this: ["YER-CGH-JOP", "XXX-XXX-XXX"]

Since these are the only ones that match the formula and characters allowed.

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115
  • Where does the pattern (eg `XXX-XXX-XXX`) and the allowed characters come from? Can they be retrieved in code? – Rory McCrossan Nov 19 '15 at 16:04
  • So the patterns and characters allowed are coming from other input fields that the user types in, so the formula can change. But the formula will always only have X's and hyphens in it. The chrs variable (characters allowed) also comes from user. So, we get these values within the function that executes this code dynamically at runtime. – Solomon Closson Nov 19 '15 at 16:05
  • Can not only match characters A-Z, because the user can change these characters to anything really. The `chrs` variable could be: `0123456789ABZ`, or even `AB&XY$H5` – Solomon Closson Nov 19 '15 at 16:09
  • Another regex option....`var re = new RegExp("[" + chrs + "]{3}-?[" + chrs + "]{3}-?[" + chrs + "]{3}");` – 001 Nov 19 '15 at 16:23

1 Answers1

2

It's a little unsightly, but you could use the string of allowed characters and the provided pattern string to build a Regex using string replacement of the X. From there you can loop over the inputs and test the values against the expression. Something like this:

var chrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // assume this is retrieved from a user input
var format = 'XXX-XXX-XXX'; // assume this is retrieved from a user input
var re = new RegExp('^' + format.replace(/X/g, "[" + chrs + "]") + '$');

var currMatches = $(".p-serials").map(function () {
    if (re.test(this.value))
        return this.value;
    return;
}).get();

Example fiddle

If you want to make the Regex case-insensitive, you can add the flag to the constructor:

new RegExp('^' + format.replace(/X/g, "[" + chrs + "]") + '$', 'i')

Also note that you would need to do a lot of string sanitisation on this to ensure the user input doesn't interfere with recognised Regex operators.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Thanks, so basically any special characters should be removed? – Solomon Closson Nov 19 '15 at 16:26
  • It depends on your requirements, but I would at least remove backslashes. See the regex cheatsheet [here](http://regexr.com/) for a listing of what you need to look out for, – Rory McCrossan Nov 19 '15 at 16:27
  • I have found bug in your code here: http://jsfiddle.net/jgkh8nLm/1/ Why is it returning all values? The format is obviously different. – Solomon Closson Nov 19 '15 at 16:39
  • Good find. It's because the Regex was global, so would match any point of the string. I have updated it to include string start and end delimiters: http://jsfiddle.net/jgkh8nLm/2/ – Rory McCrossan Nov 19 '15 at 16:40
  • Looks great. Need `+` before `'$'` above tho. Thanks, this seems to be exactly what I need! You're the BEST! – Solomon Closson Nov 19 '15 at 16:52