How to Use JavaScript to Search Text for Keywords

From Q
Jump to navigation Jump to search

This code snippet can be used to make a filter which selects all respondents whose Text response matches a particular string (or strings). To use it, modify the first line of code to enter the desired search terms, and modify the second line to replace MY_INPUT_TEXT with the Variable Name of the Text variable you want to search. The text will be converted to lowercase, so you don't need to worry about searching for the same word in different cases. Once you have created your variable, make sure it is tagged as a Filter.

var _search_terms = ["hello", "hi"];
var _text = MY_INPUT_TEXT;
_text = _text.toLowerCase();
var _term_found = _search_terms.some(function (_term) { return _text.indexOf(_term) > -1; });