簡體   English   中英

如果我復制並粘貼它,為什么 console.log() 沒有任何內容?

[英]Why doesn't it console.log() anything if I copy and paste it?

我在Glitch上運行此代碼以在對話之前添加 \\n,如果我在單詞所在的空間中輸入內容 toFormat 它可以正常工作,但是如果我將某些內容復制並粘貼到其中,它不會 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())

}

問題是您試圖拆分"但字符串包含 ,這是一個不同的 ASCII 字符。常見的復制 + 粘貼問題。

更改 toformat 中的字符有效:

/* !!! */
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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM