Against frustration with native RegExp's.
For the new Substance.Surface
we had to implement a simple lookup method that scans for word boundaries
in a paragraph (in order to find out where to jump when pressing
right in our text editor).
So we came up with a simple regular expression.
var wordBounds = /\b\w/g;
In order to get all matches of a regular expression on a string you have to call
RegExp.exec multiple times, which is quite error-prone and terrible to write.
var str = "Fix problem quickly with galvanized jets";var matches = ;// Execute until last match has been foundwhile match = wordBoundsexecstr !== nullmatchespushmatch;
It's also quite unintuitive to access the information you need. In our case we needed to know the character positions of the matched word boundaries. Inspired by Phython and Ruby we came up with something that feels more natural, but without adding any magic.
var wordBounds = /.\w/g;var matches = wordBoundsmatchstr;
Much better. Now if our cursor were at position 6 and we wanted to jump to next word bound we could find out the position by inspecting the normalized matches.
var cursorPos = 6;var nextBound = _findmatchesreturn mindex>charPos;index;
Easy. If you want to see it in action have a look at the brand-new Selection API.