Почему это не console.log () ничего, если я копирую и вставляю это? - PullRequest
0 голосов
/ 09 января 2020

У меня есть этот код, запущенный на Glitch , чтобы добавить \ n перед диалогом, если я набираю что-то в пространство, где слова находятся в формате, он работает нормально, но если я копирую и вставляю что-то в него, это не console.log что-нибудь

/* !!! */
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())

}

1 Ответ

0 голосов
/ 09 января 2020

Проблема в том, что вы пытаетесь разделить на ", но строка содержит , который является другим символом 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.
...