如果我复制并粘贴它,为什么 console.log() 什么都没有?
Why doesn't it console.log() anything if I copy and paste it?
我在 Glitch 上有这段代码 运行 可以在对话前添加 \n ,如果我在 space 中输入一些东西,其中单词在 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 字符。常见的复制+粘贴问题。
更改格式中的字符有效:
/* !!! */
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.
我在 Glitch 上有这段代码 运行 可以在对话前添加 \n ,如果我在 space 中输入一些东西,其中单词在 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 字符。常见的复制+粘贴问题。
更改格式中的字符有效:
/* !!! */
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.