Search

expand-selection

Expand Selection

If the left hand edge of a selection is in side a word, move it further left until we hit the edge of the word. Likewise if the right hand edge of the selection is in a word, expand it right until we hit the edge of the word.

const expandSelection = (s, i, j) => {
  let m

  if( j === undefined ) j = i
  if( i > j ) { let t = i; i = j; j = t; }

  const al = s.substr(0, i)
  const l = (m = al.match(/^(.*\s|)(\S+)$/s)) ? m[1].length : i 

  const ar = s.substr(j)
  const x = (m = ar.match(/^(\S+)/)) ? m[0].length : 0
  const r = x + j

  return [l,r]
}