Is there a way to cut the value by line numbers? For example, here's my editor's text area:
initial function () {
a = '1';
b = '2';
}
I will use getRange() to get the value inside initial function from editor, here's what I want to get:
a = '1';
b = '2';
Right now I want to append text to each line of value:
let val = cm.getRange({line: 0, ch: 0}, {line: cm.lineCount()-1, ch: 0});
val = 'someText' + val.replace(/\s/g, '');
I will get something like this
someTexta='1';b='2';
But what I want is
someTexta = '1';
someTextb = '2';
Is there a way in codemirror to cut value into array or something else by lines?
You can split your string into separate lines using .split(/\n/)
. This will give you an element in an array for each line. Then, you can .map()
each line into a string with "someText
prepended to it (only if that line is valid - hence the line ?
check). Then, you can convert it back into a string using .join('\n')
. See example below:
let val = ` a = '1';
b = '2';`;
val = val.split(/\n/).map(line => line ? "someText" +line.trim() : line).join('\n');
console.log(val);