Wordcount of an embedded data field | XM Community
Solved

Wordcount of an embedded data field

  • 19 April 2021
  • 1 reply
  • 8 views

Userlevel 1
Badge +1

Hi community!
I have an embedded data field,

high_string
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

icon

Best answer by GrumpyCivet 21 April 2021, 15:32

View original

1 reply

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)

Leave a Reply