Last active
January 30, 2024 15:23
-
-
Save JunkMeal/70732834ed493aeea8fe018ca6959f46 to your computer and use it in GitHub Desktop.
A great function for any kind of text extraction. I mainly use it for HTML. I write it a LOT so here it is!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function selectin(str, startstr, endstr) { | |
const starti = str.indexOf(startstr) + startstr.length | |
if (starti == -1) return -1 | |
const endi = str.indexOf(endstr, starti) | |
if (endi == -1) return -1 | |
return str.substring(starti, endi) | |
} | |
function selectinall(str, startstr, endstr) { | |
let result = [] | |
let starti = 0 | |
while (starti < str.length) { | |
const startIndex = str.indexOf(startstr, starti) | |
if (startIndex == -1) break | |
const endIndex = str.indexOf(endstr, startIndex + startstr.length) | |
if (endIndex == -1) break | |
const selectedText = str.substring(startIndex + startstr.length, endIndex) | |
result.push(selectedText) | |
starti = endIndex + endstr.length | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment