gpt4 book ai didi

javascript - ACE EDITOR 查找文本、选择行并替换文本

转载 作者:数据小太阳 更新时间:2023-10-29 05:22:51 28 4
gpt4 key购买 nike

Ace Editor ,我想找到一个文本,选择行并替换该文本。我可以找到文本(使用自己的标签找到它们),并且它正在工作。我还可以使用以下代码获取行(行)号:

editor.find('needle',{
backwards: true,
wrap: true,
caseSensitive: true,
range: null,
wholeWord: true,
regExp: false
})
editor.$search.set({
needle: /(start_#D1_SavePos)/
});
var found = editor.$search.find(editor.getSession()),

Range = require('ace/range').Range,

// find tagname "start_#D1_SavePos" in editor
mine = new Range(found.start.row+1, found.start.column-1, found.end.row+1, found.end.column),

// read the line number
D1SavePos = (editor.session.getTextRange(mine)),

// get next line number, after "start_#D1_SavePos"
// I need only this and in the editor it will be R13=0
rowOfD1SavePos = (mine+1),

// Rewrite R13 with value from input text
// get Value from input field
newD1SavePos = document.getElementById("highestBTTXT").value,

//Replace Zero-Value from R13 to new value
D1SavePos = D1SavePos.replace(/R13=0/, "R13=" + newD1SavePos);

// Now set this to editors row

尝试进行行选择,因为在“rowOfD1SavePos”中存储了读取的行号:

    editor.selection.moveCursorToPosition(rowOfD1SavePos);

但它不起作用。如果这可行,我想用值“D1SavePos”替换这一行

更新 25.10.14

这是我完美的工作解决方案:

editor.find('needle',{
backwards: true,
wrap: true,
caseSensitive: true,
range: null,
wholeWord: true,
regExp: false
});

editor.$search.set({ needle: /(start_#D1_SavePos)/ });

var found = editor.$search.find(editor.getSession()),

Range = require('ace/range').Range,

// Find Coordinates, after Line "start_#D1_SavePos"
row = new Range(found.start.row+1, found.start.column-1, found.end.row+1, found.end.column),

// read Text in row
D1SavePos = (editor.session.getTextRange(row)),

// rewrite
// get value from input field
newD1SavePos = document.getElementById("highestBTTXT").value;

// rewrite R-Parameter with input value
D1SavePos = D1SavePos.replace(/R13=0/, "R13=" + newD1SavePos);

// write in editor
// mark line
editor.selection.moveCursorToPosition({row: row, column: 0});
// write
editor.session.replace(new Range(row, 0, row, Number.MAX_VALUE), D1SavePos);

最佳答案

moveCursorToPosition 获取一个位置,该位置是一个具有行和列的对象 editor.selection.moveCursorToPosition({row: row, column: 0}) ;

要替换文本,请像这样使用 editor.session.replace(range, text)

var range = editor.find('needle',{
wrap: true,
caseSensitive: true,
wholeWord: true,
regExp: false,
preventScroll: true // do not change selection
})
range.start.column = 0
range.end.column = Number.MAX_VALUE
editor.session.replace(range, "x" + editor.session.getLine(range.start.row) + "x")
editor.selection.setRange(range)

注意:在你的例子中 mine + 1 是一个字符串,你会很容易地明白为什么 editor.selection.moveCursorToPosition(rowOfD1SavePos); 如果你使用浏览器开发工具查看该函数源

关于javascript - ACE EDITOR 查找文本、选择行并替换文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26555492/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com