Skip to content

Instantly share code, notes, and snippets.

View donbrae's full-sized avatar

donbrae

View GitHub Profile
@donbrae
donbrae / Useful regular expressions.md
Last active April 8, 2024 10:58
Some regular expressions for finding patterns in text.
  • Find all references to filenames ending in .js: [^ ]+\.js
  • Find all blank lines: ^\r?\n\r?
  • Wildcard (any character and newlines): (.|\n)+?
    • Eg find all <td>s, regardless of content: <td>(.|\n)+?</td>
      • Include attribute(s): <td(.|\n)+?>(.|\n)+?</td>
  • Convert getComputedStyle(foot1).left to foot1.getBoundingClientRect().x: find getComputedStyle\((.+?)\).left and replace it with $1.getBoundingClientRect().x
  • Remove all <a> tags, leaving just the anchor tag text: find <a[^>]*>(.*?)</a> and replace with $1
    • Only links that start with the string /foo: <a href="/foo[^>]*>(.*?)</a>
  • Find all URLs: \b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]
    • All URLs ending in .jpg: \b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|](\.jpg)
@donbrae
donbrae / Reactive UI with Proxy object.html
Last active March 8, 2023 08:24
Reactive UI with Proxy object
<!DOCTYPE html>
<html lang="en">
<head>
<title>Reactive UI with Proxy object</title>
</head>
<body>
<!-- Comment which got me thinking: https://news.ycombinator.com/item?id=35062021 -->
<p>Count:
<span id="count"></span>
</p>
@donbrae
donbrae / similarText.js
Last active February 17, 2025 01:52
JavaScript function which mimics PHP’s `similar_text()`.
// Source: ChatGPT 4
function similarText(first, second) {
// Check for null, undefined, or empty string inputs
if (first === null || second === null || typeof first === 'undefined' || typeof second === 'undefined' || first.trim().length === 0 || second.trim().length === 0) {
return { matchingCharacters: 0, similarityPercentage: 0 };
}
// Type coercion to ensure inputs are treated as strings
first += '';
second += '';