Skip to content

Instantly share code, notes, and snippets.

@JunkMeal
Last active January 30, 2024 15:23
Show Gist options
  • Save JunkMeal/70732834ed493aeea8fe018ca6959f46 to your computer and use it in GitHub Desktop.
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!
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