const regex = /site[\s\S]{0,100}\/a>/g;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('site[\\s\\S]{0,100}\\\/a>', 'g')
const str = `<body>
<!-- Websites navigation menu -->
<ul class="navbar">
<li><a href="index.html">Home page</a>
<li><a href="musings.html">Musings</a>
In the following code, what is the regex (within Sublime's search) for finding "site" (seen within "Websites") and /a> (seen within ) only when there are between 0 and 50 characters of any type in between those two strings? I'd like to be able to perform searches like this on a regular basis, just changing out the two keywords and a distance, thanks.`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions