如何使用JavaScript和jQuery在CKEditor 5中找到和替换文本?
我想在文本中找到特殊字符'@',并在我自己的文本中替换'@'和'@'字符之后的所有字符。
我使用change:data…
window.editor.model.document.on('change:data', () => {
});
您需要editor.getData()
和editor.setData()
,然后使用Regex@w+
来匹配@
,alphanumeric
和space
,没有匹配空间,它只适用于粘贴,但您不能键入@user
,因为键入@u
时它将被newString
替换
下面的例子
ClassicEditor
.create(document.querySelector('#editor'))
.then(editor => {
editor.model.document.on('change:data', () => {
let content = editor.getData();
if (/@w+( |s)/gi.test(content)) {
content = content.replace(/@w+( |s)/gi, 'newString ')
editor.setData(content)
}
})
})
<script src="https://cdn.ckeditor.com/ckeditor5/34.0.0/classic/ckeditor.js"></script>
<div id="editor"></div>