I have just started using mark.js (https://markjs.io), and right now I am trying to find the right RegEx to capture as little data as possible (non-greedy type), and no more than a certain number of characters.
I tried multiple options, and so far I have these three regular expressions, but each has its own faults:
w(.{1,30})?3
- captures 'word1 word2 word3 wo rd3', instead of 'word3' and 'wo rd3';w(\w{1,30})?3
- captures 'word3' as it should, but fails for 'wo rd3';w((\w| ){1,30})?3
- this behaves exactly like the 1st option above.
For a better understanding, please run the code below.
What do you think, what am I missing here, please?
Alex
var regex = /w(.{1,30})?3/i;
// var regex = /w(\w{1,30})?3/i;
// var regex = /w((\w| ){1,30})?3/i;
var instance = new Mark(document.querySelector("body"));
instance.markRegExp (regex, {
className: "mark"
});
.mark {
color: white;
background: red;
}
<script src="https://cdn.jsdelivr.net/mark.js/8.8.3/mark.min.js"></script>
<div>word1 word2 word3 wo rd3 word4</div>