I need to get the line number of specific string in the textarea which has codemirror plugin.
var editor= CodeMirror.fromTextArea(document.getElementById("refstyle");
var doc = editor.getDoc();
var cursor = doc.getCursor(); // gets the line number in the cursor position
var line = doc.getLine(cursor.line); // get the line contents
editor.getSelection(line number) //it will select the text based on line number
So, How can I get the line number based on string (I expect something like doc.getLine(string) or doc.getSelection(String))
Is there any function to get the line number based on the string or to get searched string selected based on line number in the textarea? Pls help!
To search inside a CodeMirror instance, use the searchcursor addon. It'll give you the line/char position of matches.
Related
I'm trying to use TextFinder, but in my spreadsheet, I don't want to return the header row or column 1 in my results. I see that TextFinder has a "startFrom" method that's supposed to let you define "The cell range after which the search should start" ... but I can't seem to make it work.
var tf=ss
.createTextFinder(text)
.useRegularExpression(true)
.ignoreDiacritics(true)
.startFrom(ss.getRange("B2"));
var all=tf.findAll();
What am I doing wrong? I get results, they just still include matches from row 1 and column A.
Assuming ss is spreadsheet, you're creating a text finder for the entire spreadsheet. startFrom(range) only says
The cell range after which the search should start
If you use getCurrentMatch() or findNext() it'll return the next range matching your text criteria starting from that range(i.e., Sheet1!B2). If you want a textFinder specific to a range, then create the text finder on that range:
var tf = ss
.getSheetByName('Sheet1')
.getRange('B2:B')
.createTextFinder(text)
I have a list of grades on web page with identical span class. I Would need to append the current numeric grade and add a alphabet grade after the number. Current information is a text field and i would first need to parse the first 3 characters to identify the number. Then i would need to create a 5 level condition to give correct alphabet based on the number (example 0-40 = D, 40-60 = C, 60-80= B and 80-100 = A). Im newbie and just cant make it to work.
Grades list
I am able to update update the text, but when trying to parse and create conditions just was not able to make it work.
var elements = document.querySelectorAll('.score_value');
for ( var i=elements.length; i--; ) {
elements[i].textContent += "something else";
}
all the help would be appriciated
To parse integer in javascript, use parseInt() then include the parsed result in a condition to append what you want to text.
I want to split the value of a textarea with \n and make the line where the cursor is positioned as the last value in the array for example:
1. fyg tgiyu rctvyu cuiby cutv cutrvyb crtvyb
2. rutyu rtcvyb ctrvybu ctrvybu rtcvy
3. rutiyu crtvyu crtvyb rtvyb
4. |
5. tgyho8uji vtybui
6. tvybui yivtubi
Now the numbers are the lines in the textarea and line 4 is the line where the cursor is positioned. so I want to split the lines ignoring line 5 and 6 have line 4 as the last line. Then I will run a code like this:
lastLine = //the position of the cursor
if(lastLine == ""){
console.log('empty');
} else {
//get the value of the previous line before the lastLine
}
Please how do I achieve this using jQuery or JavaScript
You can achieve this by the jQuery substr().
You select the substring of the textarea value from the starting point to the point where the cursor is located and split it with line breaks. Like this;
value = $('textarea').val();
// to get the position of the cursor
index = $('textarea').prop('selectionstart');
// select all from the starting point to the cursor position ignoring line 5 and 6
str = value.substr(0, index);
// split the str with a line break
splt = str.split('\n');
// then finally to get your last line
lastLine = splt[splt.length - 1];
$('#theTextArea').prop("selectionStart");
and
$('#theTextArea').prop("selectionEnd");
The above properties give you the location of the cursor on modern browsers. This question has been linked here before though. For older browsers it could be problematic.
But given the cursor position you should be able to just grab the substring to the cursor index. Then use a regex to get the contents of the last line to the end of the string.
I need to replace all the matches of a regular expression till the caret position in a textarea using Javascript.
For example, if the text in the textarea is: "6 students carry 2 books to 5 classes" and the cursor
is placed on books and the regular expression is /\d/, the numbers 6 and 2 should be replaced by, say, 4.
I know the replace function and I know how to get the caret position, but how do I solve this problem?
Thanks for any help in advance!
textareaClicked = function(e){
var pos = e.target.selectionStart;
var beforeSelection = e.target.innerHTML.slice(0,pos);
var afterSelection = e.target.innerHTML.slice(pos);
var newHTML = beforeSelection.replace(/\d/g,4) + afterSelection;
e.target.innerHTML = newHTML;
e.target.setSelectionRange(pos,pos);
};
document.getElementById('foo').onclick=textareaClicked;
see it in action in this jsfiddle.
There is probably a more elegant way, but I would just copy the text from the text area, split the string into two substrings at the caret position (which you said you know how to find), do the replace on the first substring and then concatenate it with the second substring. Copy it back into the text area making sure to update the caret position appropriately.
I have a jQuery plugin that finds the caret position of a textarea. I implement it on the keyup function of the textarea, like this:
$("#editor").keyup(function () {
var textbox = $(this);
var end = textbox.getSelection().end;
});
I am wanting to find the word, or part of a word, before the caret. Words are delimited by any type of whitespace.
My main difficulty in doing this is dealing with line breaks. How can I find the word or part of a word immediately before the caret given the character index of the caret?
If you're using my jQuery textarea plug-in for this, the selection character positions are always relative to the value property of the textarea, regardless of how the browser handles line breaks. You could then get the last word as follows:
Note that you have to use the textarea's value property and not jQuery's val() method, which normalizes line breaks.
$("#editor").keyup(function () {
var textbox = $(this);
var end = textbox.getSelection().end;
var result = /\S+$/.exec(this.value.slice(0, end));
var lastWord = result ? result[0] : null;
alert(lastWord);
});