of the form: "2: Pickles, 4:dog., 5: bottle, 13: rainbow." I need to set a new embedded data field,
string_length
that captures the wordcount (ignoring numbers) of this field (i.e., in the example above, it should be 4 since it's "pickles", "dog", "bottle" & "rainbow"). Anyone know how I can do this? Thanks! I don't know JS or regex, but I know it'll involve both. I suspect Qualtrics.setEmbeddedData & str.split
Best answer by GrumpyCivet
If I'm understanding correctly, here you use comma "," as the delimiter. So you would just need to split by "," and count the length. var myString = Qualtrics.SurveyEngine.getEmbeddedData("high_string");
var wordCount =0;
wordCount = myString.trim().split(',').length;
Qualtrics.SurveyEngine.setEmbeddedData("word_count", wordCount); Just put this code somewhere after your
high_string
is set. This should work for you as long as comma will not appear in your "word". The variable
wordCount
captures the number of words there, and the last line sets an embedded data called
word_count
to store it. (P.S. "2: Good day, 3: Nice" will be counted as 2 "words" instead of 3)
If I'm understanding correctly, here you use comma "," as the delimiter. So you would just need to split by "," and count the length. var myString = Qualtrics.SurveyEngine.getEmbeddedData("high_string");
var wordCount =0;
wordCount = myString.trim().split(',').length;
Qualtrics.SurveyEngine.setEmbeddedData("word_count", wordCount); Just put this code somewhere after your
high_string
is set. This should work for you as long as comma will not appear in your "word". The variable
wordCount
captures the number of words there, and the last line sets an embedded data called
word_count
to store it. (P.S. "2: Good day, 3: Nice" will be counted as 2 "words" instead of 3)