I need a JavaScript regex to:
- Return the first X words in a string, within the first 15 characters.
- A word boundary needs to be treated as anything that is not alphanumeric, so any space or special character.
- Any trailing spaces or special characters should not be returned in the match.
I currently have the following:
var regex = /(.{1,15})\b/
Which is satisfying 1 & 2, but not 3.
var matches = 'when-winding/order-made'.match(regex);
console.log(matches[1]); // logs 'when-winding/', but I want 'when-winding'
How can I modify this regex to achieve the desired result?
Here's a fiddle with a more comprehensive example: http://jsfiddle.net/jqs9Lnr0/