0

I have a string like this:

var str = "this is test1
           this is test2
           this is test3";

Now I want if both sides of that range are \n then returns true, else return false. In the above example just these three ranges are true:

[0 - 12] => true

[14 - 26] => true

[27 - 39] => true

And all other ranges have to be false. Like these: [1 - 12], [5 - 17], ...


Note: Spaces before and after that range doesn't matter. For example:

var str = "     this is a test    ";

Now this ranges are true: [0 - 20], [2 - 18], [5 - 22], ...


In reality, I'm trying to create a markdown-editor and now I'm working on its code-method button. So I need to know, if all of selected text (that range) is in a line, then append 4spaces before it, else append two "`" surround it.

stack
  • 10,280
  • 19
  • 65
  • 117
  • Why wouldnt you just split the text on new lines and do what you need with the resultant array? – Wesley Smith Jan 26 '16 at 05:50
  • @DelightedD0D I don't get it... But there is two cases ... appending four spaces before selected text (if all of that be on a line) or appending two colon surround it .. (if selected text is in the middle of line) – stack Jan 26 '16 at 05:53
  • How is "range" determined ? – guest271314 Jan 26 '16 at 06:09
  • @guest271314 I have that range. I just need to detect all of that range is in a line or not .. – stack Jan 26 '16 at 06:10
  • Tried using `$` _"Matches end of input. If the multiline flag is set to true, also matches immediately before a line break character."_ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#boundaries ? – guest271314 Jan 26 '16 at 06:12
  • If requirement is to determine if last character of selected text is end of line , could use `$` at `RegExp` ? – guest271314 Jan 26 '16 at 06:15
  • @guest271314 Yes requirement is what you said plus if there is some spaces between the last character of selected text and end of line then still is should be `true` – stack Jan 26 '16 at 06:18
  • Why use a regex at all? Search for the newline character with normal string functions, e.g. at position `15` and check if this is in the range `0-14` (no, it isn't). – Jan Jan 26 '16 at 06:18
  • @guest271314 btw, I need to check both sides of selected text *(that range)*, not just the end of that. – stack Jan 26 '16 at 06:18
  • @Jan Do you mean I check all before and after character until a `\n` using a `loop` and `if`? – stack Jan 26 '16 at 06:19
  • @stack See post. Try selecting all contents of `textarea` element at stacksnippets, results at `console` – guest271314 Jan 26 '16 at 06:34

1 Answers1

1

Try using onselect event , storing each line of input text within an array, checking each line for saved variable using RegExp \n . See also Getting currently selected text

var textarea = document.querySelector("textarea"), re = /\n/;

textarea.onselect = function(e) {
  var res = [];
  // current selection
  var sel = this.value.slice(this.selectionStart, this.selectionEnd);
  // create array of input value split at new line characters
  var matches = this.value.split(re);
  // split input value at new line characters
  // return `true` or `false`
  var selected = sel.split(re);
  for (var i = 0; i < selected.length; i++) {
    if (matches.indexOf(selected[i]) !== -1) {
      res.push(true)
    } else {
      res.push(false)
    }
  }

  console.log(matches, res, selected)
}
<textarea style="width:300px;height:200px">
  this is test1
  this is test2
  this is test3</textarea>
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • It returns `true` just for last line of textarea .. Am I write? – stack Jan 26 '16 at 06:36
  • @stack _"It returns true just for last line of textarea .. Am I write? "_ Yes. Is this expected result ? – guest271314 Jan 26 '16 at 06:37
  • I want if all of selected text is in a line then return `true`. So, in your example, it should returns `true` for three cases: 1. if all of first line is selected, 2. if all of second line is selected, 3. is all of third line is selected – stack Jan 26 '16 at 06:39
  • @stack Same process. Reference entire `textarea` `value` , compare if selected text matches each line of `textarea` value – guest271314 Jan 26 '16 at 06:41
  • It returns `true` just for first line, and also it returns `[true, true, true]` if I select all three lines together .. otherwise returns `false` – stack Jan 26 '16 at 06:46
  • See updated post. Note, matches spaces within line as part of selection. Could use `.trim()` on variables to remove this portion of check – guest271314 Jan 26 '16 at 06:54
  • Well, Now seems perfect ... Thanks +1 – stack Jan 26 '16 at 06:56
  • Just one thing, your code returns a separated `true` or `false` for each line. May you please tell me how can I implement it which returns just one `true` or `false` in total? – stack Jan 26 '16 at 07:01
  • Try using `.some()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some or `.every()` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every – guest271314 Jan 26 '16 at 07:07
  • Look, every thing is fine ... Just the spaces surround selected text acts like characters .. I want when you select `this is test1` *(without the spaces around it)* from [this](https://jsfiddle.net/jmwe433g/1/), it returns `true`, but it returns `false`, because there is some spaces surround selected text. Do you know how can I fix it? – stack Jan 26 '16 at 17:59
  • Very good, thanks pal. That fiddle is exactly what I needed. – stack Jan 26 '16 at 20:43
  • Oh ... A really odd thing: Please open [this fiddle](https://jsfiddle.net/jmwe433g/11/) and select second `sa` in the second line. It returns `true` ... Why? Do you know? – stack Jan 27 '16 at 19:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101810/discussion-between-guest271314-and-stack). – guest271314 Jan 27 '16 at 19:30