How to split a string using 2 different separators | XM Community
Skip to main content
I am trying to break up a date text string into the year, month, and day, but I just discovered that the date text string can separate the values using either "-" or "/" depending on the source. The script I am using (adopted from a script I got in an earlier thread) will work for one, either/or the other.



Is there a way to write a .split statement with an or condition? Below is my code.



Imagine that COVERAGE_BEG_DT can be either 01/01/2018 or 01-01-2018



const Coverage_Beg_DT = Qualtrics.SurveyEngine.getEmbeddedData('COVERAGE_BEG_DT');



Coverage_Beg_DTArr = Coverage_Beg_DT.split("/")

Qualtrics.SurveyEngine.setEmbeddedData("YOTenure",Coverage_Beg_DTArr[2])

Qualtrics.SurveyEngine.setEmbeddedData("MOTenure",Coverage_Beg_DTArr[0])

Qualtrics.SurveyEngine.setEmbeddedData("DOTenure",Coverage_Beg_DTArr[1])
Use a regex split: `string.split(/[-\\/]/);`
@TomG - Thanks!



I am still learning javascript, so can you explain how to do that?



My guess would be:

Change

Coverage_Beg_DTArr = Coverage_Beg_DT.split("/")

To

Coverage_Beg_DTArr = Coverage_Beg_DT.string.split(/[-\\/]/);



I am not sure what all the pieces are doing there... such as "/" at the start and finish, and does "\\" function as a separator? Sorry if these are simple questions.
Putting it inside / and / makes it a regex expression (in js). Putting it in [ and ] means match any of these characters. Since / is a reserved character is has to be escaped with \\\\.



Regex is confusing, but there are many Internet resources out there to learn what you need.
@TomG - Thank you so much. I really appreciate the help 😀

Leave a Reply