How is it possible to find some random element in some code pasted into ace.js editor? Example:
editor.find("<[a-z]+>",{regExp:true});
On a XML-Code like this, the statement will only find the first element
<root>
<element1></element1>
<element2></element2>
<element3></element3>
</root>
How is it possible to find a random element? e.g. element1, 2 or 3...
BTW: ace.js is providing a way to find all of these elements.
Thank you!
you can find all ranges and pick one of them randomly
editor.$search.setOptions({needle: "<[a-z]+", regExp:true, preventScroll: true});
ranges = editor.$search.findAll(editor.session)
randomRange = ranges[Math.floor(Math.random() * ranges.length)]
if (randomRange)
editor.selection.setRange(randomRange)
or randomly pick starting position
i = Math.floor(Math.random() * editor.getValue().length)
startPos = editor.session.doc.indexToPosition(i)
editor.find("<[a-z]+",{regExp:true, start: startPos})
update: the second method isn't good since it's distribution isn't uniform