mardi 31 août 2021

Is there any difference between clipboardData.getData('text/plain') and general plain text?

In pure javascript file we have:

let plainText = `a
b`
// anb
console.log(plainText.replace(/\n/, 'n'))
// nb
console.log(plainText.replace(/(.+)\n/, 'n'))

Then paste the plain text

a
b

into the following div:

div{
  border: 1px solid #000;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div contenteditable="true"></div>
    <script>
        const div = document.getElementsByTagName('div')[0]
        div.addEventListener('paste', (e) => {
            let plainText = e.clipboardData.getData('text/plain')
            // a
            // b
            console.log(plainText)
            // anb
            console.log(plainText.replace(/\n/, 'n'))
            // a
            // b
            console.log(plainText.replace(/(.+)\n/, 'n'))
        })
    </script>
</body>

</html>

Why does plainText.replace(/(.+)\n/, 'n') get different results in javascript and web page? (CRLF is used in the editor. The browser runs on win10.)

The problem occurs in the latest Chrome and Edge while Firefox works fine.




Aucun commentaire:

Enregistrer un commentaire