Проблема в том, что вы пытаетесь разделить на "
, но строка содержит “
, который является другим символом ASCII. Распространенная проблема копирования + вставки.
Изменение символов в работах формата:
/* !!! */
let toformat = `
"Excuse me," he said. The young woman didn't respond. "What are you doing?" "Hey, I’m talking to you" Rudy said.
`
/* !!! */
let truef = false
/* !!! */
let run = true
/* !!! */
if(run){
let res = []
let dialogue = []
let other = []
let split = toformat.split('"').join('|"').split('|').slice(1)
split.forEach((x, i) => {
if(i % 2 == 1){
other.push(x)
} else {
dialogue.push(x)
}
})
for(let i = 0; i < dialogue.length; i++){
res.push('\n' + dialogue[i] + '"')
res.push(other[i].slice(1))
}
console.log(res.join(''))
console.log(Math.random())
}
Результат:
"Excuse me," he said. The young woman didn't respond.
"What are you doing?"
"Hey, I’m talking to you" Rudy said.