admin管理员组文章数量:1336632
Objective: I am using codemirror as editor. I want to
- Search and highlight multiple strings
- I want to be able to iterate each number of matches found and print its line number.
- I want to do it programatically and do no want to use dialog as in example .html
Issue:
- during while loop only last match is selected, previous ones are cleared , but i also want it hightlighted yellow like .html
JSFIDDLE: /
CODE:
$(document).ready(function() {
//
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: "text/html",
lineNumbers: true,
});
//
function search(val) {
var cursor = editor.getSearchCursor(val);
while (cursor.findNext()) {
editor.setSelection(cursor.from(), cursor.to());
console.log('found at line ', cursor.pos.from.line + 1);
}
}
//
$('#search').click(function(event) {
event.preventDefault();
search(/^alpha|^beta/);
});
//
});
Objective: I am using codemirror as editor. I want to
- Search and highlight multiple strings
- I want to be able to iterate each number of matches found and print its line number.
- I want to do it programatically and do no want to use dialog as in example https://codemirror/demo/search.html
Issue:
- during while loop only last match is selected, previous ones are cleared , but i also want it hightlighted yellow like https://codemirror/demo/search.html
JSFIDDLE: https://jsfiddle/bababalcksheep/p7xg1utn/30/
CODE:
$(document).ready(function() {
//
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: "text/html",
lineNumbers: true,
});
//
function search(val) {
var cursor = editor.getSearchCursor(val);
while (cursor.findNext()) {
editor.setSelection(cursor.from(), cursor.to());
console.log('found at line ', cursor.pos.from.line + 1);
}
}
//
$('#search').click(function(event) {
event.preventDefault();
search(/^alpha|^beta/);
});
//
});
Share
Improve this question
asked Mar 2, 2019 at 9:58
djangodjango
2,9096 gold badges51 silver badges83 bronze badges
1 Answer
Reset to default 8Calling setSelection
can only highlight one continuous substring at a time. Instead, you can use the markText method for this, passing in cursor.from()
and cursor.to()
to get the locations you want to highlight:
$(document).ready(function() {
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
mode: "text/html",
lineNumbers: true,
});
function search(val) {
var cursor = editor.getSearchCursor(val);
while (cursor.findNext()) {
editor.markText(
cursor.from(),
cursor.to(),
{ className: 'highlight' }
);
}
}
//
$('#search').click(function(event) {
event.preventDefault();
search(/^alpha|^beta/);
});
});
.CodeMirror {
border-top: 1px solid black;
border-bottom: 1px solid black;
height: 200px;
}
.highlight {
color: orange;
}
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/codemirror/5.44.0/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/codemirror/5.44.0/addon/search/searchcursor.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare./ajax/libs/codemirror/5.44.0/codemirror.min.css">
<div class="container">
<p><strong>Objective:</strong></p>
<p>Find/search and highlight both words <strong>alpha</strong> and <strong>beta</strong> in codemirror editor</p>
<button id="search" type="button" class="btn btn-primary">Search and highlight</button>
<br><br>
<textarea id="code" name="code" rows="8">Text line
alpha 1
Text line
Text line
alpha 2
Text line
Text line
beta 1
Text line</textarea>
本文标签: javascriptcodemirror search and highlight multipule words without dialogStack Overflow
版权声明:本文标题:javascript - codemirror: search and highlight multipule words without dialog - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742412637a2470094.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论