Search code examples
jqueryopenai-api

how to split delimiter with numbers


I am using openai to get subject lines, choices returning string not array/object, i need to split string by numbers.

var choices = "1. "Don't Miss Out: Exclusive Deals Inside!" 2. "Hurry! Limited Time Offer Inside" 3. "Unlock Savings: Open This Email Now" 4. "Last Chance to Save Big"";
choices = choices.split('/[0-9]+\./');
console.log(choices)

this is not working for me. its showing all string into single array index.

i needed output as

["Don't Miss Out: Exclusive Deals Inside!","Hurry! Limited Time Offer Inside","Unlock Savings: Open This Email Now","Last Chance to Save Big"]

Solution

  • Define the RegExp without quotes:

    const choices = `1. "Don't Miss Out: Exclusive Deals Inside!" 2. "Hurry! Limited Time Offer Inside" 3. "Unlock Savings: Open This Email Now" 4. "Last Chance to Save Big"`;
    const choicesArr = choices.split(/\d+\./);
    console.log(choicesArr);
    

    Edit: Changed RegExp to use \d according to Roko suggestion.