Search code examples
javascriptregexace-editor

Ace editor how to find backwards?


I'm struggling with Ace editor in javascript. My forward functions is working fine:

var selectionRange = editor.getSelectionRange();
var res = editor.find(/\?\w+\b/, { backwards: false, start: selectionRange, wrap: true, caseSensitive: false, wholeWord: false, regExp: true});

When I have multiple tokens on multiple lines, it finds them in order when I do multiple search.

?a ?b ?c  // First  ?a, then ?b, then ?c, then ?d
?d

But for finding backwards, by just changing backwards to true, it only finds the first element of each line.

var selectionRange = editor.getSelectionRange();
var res = editor.find(/\?\w+\b/, { backwards: true, start: selectionRange, wrap: true, caseSensitive: false, wholeWord: false, regExp: true});
?a ?b ?c  // First  ?d, then ?a !! ?c and ?b are skipped
?d

What is the correct way of finding backwards so that I retrieve all instances?


Solution

  • Your regexp misses g flag. Either use editor.find("\\?\\w+\\b", ...) and let ace create the regexp or use editor.find(/\?\w+\b/g, ...)