I have this detection engine running in an inc.php file that is using jQuery detection on an input field and running a regex against what the user types:
..
$trigger_selector = "[name^=trigger-]";
?>
$("<?php echo $trigger_selector; ?>").on("change keyup paste blur", function() {
run_macro_detector($(this));
});
function run_macro_detector(input) {
var new_value = input.val();
var input_id = input[0].id;
var pattern = /(\$NAME\d+\$)/;
var pattern_system = /(\$.*)/;
test = pattern.exec(new_value);
test_system = pattern_system.exec(new_value);
if (test != null) {
// add new_value to name_match_array
}
if (test_system != null) {
// add new_value to macro_match_array
}
var match_array= name_match_array.concat(macro_match_array);
input.autocomplete({ source: match_array });
}
I'm thinking the regex is somewhat limiting on what I can do, but I would love to hear some suggestions on how to make this faster, less dependent on regex or if there is an easier way to do this. I have also redacted a large bit of the code and mostly just trying to get the idea across.
Another problem that I had was how to get jQuery UI autocomplete to force detection on just the "$" character but not display it as an option in the dropdown.