Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
Why did monaco-editor getValue() return an unexpected length

Here I use editor.setValue() to render a string to the editor, the result in the render view was correct

But when I use editor.getValue() again, and print the result string length, I got an unexpected result.

Here is the code.

var value = `asfdasfsa\n\nasdfasdfasdfal;k l;'k \r\n \r\n  \r\n   k 'a\r\n  skd \r\n   a\r\n   d \r\n    \r\n     sas\r\n      dffa\r\n       sd\r\n         fsd\r\n          f\r\n          sfasfdasfasfdasdffasfaf\n\n          fa ljjflkajl\n\n          false\n\n\r\n\n          A\r\n          jpakfafafafaffaffaffafafewfwe\n          `;

console.log(value.length); // 280
var editor = monaco.editor.create(document.getElementById('container'), {
    value: value,
    language: 'javascript',

    lineNumbers: 'off',
    roundedSelection: false,
    scrollBeyondLastLine: false,
    readOnly: false,
    theme: 'vs-dark'
});

console.log(editor.getValue().length) // 290

This question has been bothering me for a long time, and I still haven't found the answer

Can somebody help answer this question ah, thank you very much~~

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It looks like the difference between 280 and 290 is because Monaco is adding \r before any \n in your string that doesn't have a \r already (so it's effectively normalising all end of line characters in the string). If you change your last console.log to console.log(JSON.stringify(editor.getValue()), editor.getValue().length) you will see how the value has changed and some additional \r's have been added.

This comment suggests that Monaco takes the input string and splits it into lines (looking for \r\n or \r or \n). The value of editor.getValue() will return the individual lines, joined by the same line separator that was used when the editor was created, hence a different character count when compared to the input string.

If you paste the following code into the Monaco Playground, you will see that it adds additional \r's and therefore the string returned from the editor (editor.getValue()) is 2 characters longer than the value supplied (value):

var value = `'line 1'\n\'line 2'\r\r\n'line 3'`;

console.log(JSON.stringify(value), value.length); // "'line 1'\n'line 2'\r\r\n'line 3'" 28
var editor = monaco.editor.create(document.getElementById('container'), {
    value: value,
    language: 'javascript',

    lineNumbers: 'off',
    roundedSelection: false,
    scrollBeyondLastLine: false,
    readOnly: false,
    theme: 'vs-dark'
});

console.log(JSON.stringify(editor.getValue()), editor.getValue().length) // "'line 1'\r\n'line 2'\r\n\r\n'line 3'" 30
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!