skip to Main Content

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"]

3

Answers


  1. 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(/[0-9]+./);
    console.log(choicesArr);
    
    Login or Signup to reply.
  2. Open AI is perfectly capable of returning the output in the format you want. Instead of parsing the returned output instruct the AI to return the output to you in as a JSON array of strings.

    Login or Signup to reply.
  3. While there is the option of choices.split(/[0-9]+./) you could always do:

    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 res = choices.split(new RegExp('d+.', 'g'));
    console.log(res)

    Also, note your string is invalid. You would have better luck using template literals.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search