Regex

Regex not returning true on second

https://stackoverflow.com/questions/59694142/regex-testvalue-returns-true-when-logged-but-false-within-an-if-statement

Here lies all the regular expressions that I found useful

regex101: build, test, and debug regex

Three-Step Process (Handles ALL Cases)

Step 1: Both single digits (M/D/YYYY)

Find: ^(\d)/(\d)/(\d{4}),
Replace: 0$1/0$2/$3,

Converts any combination:

Step 2: Single month, double day (M/DD/YYYY)

Find: ^(\d)/(\d{2})/(\d{4}),
Replace: 0$1/$2/$3,

Converts:

Step 3: Double month, single day (MM/D/YYYY)

Find: ^(\d{2})/(\d)/(\d{4}),
Replace: $1/0$2/$3,

Converts:

These three regexes work for all months (1-12) and all days (1-31)! The \d wildcard matches any digit.

get anything to the right of --

/(?<=\-\-).*/

get anything to the left of --

/^.*(?=\-\-)/

get anything between [ ]

/(?<=\[).*(?=\])/

finds string between ]( & ) → used to find string between ]( & ) i.e. [link](http://this.com)

/(?<=\]\().*(?=\))/g

replace any special characters that OS's don't like

STRING.replace(/[&#\@\!, +()$~%'":*?<>{}]/g, '_')

Only alphabet characters are allowed for this field. NO spaces

/^[aA-zZ]+$/, 

phone number validation

/^([+]?\d{1,2}[-\s]?|)\d{3}[-\s]?\d{3}[-\s]?\d{4}$/

Standard Password

/^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8,40}$/
^                         Start anchor
(?=.*[A-Z].*[A-Z])        Ensure string has two uppercase letters.
(?=.*[!@#$&*])            Ensure string has one special case letter.
(?=.*[0-9].*[0-9])        Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8}                      Ensure string is of length 8.
$                         End anchor.

Find a every line that starts with (with wildcard). Useful with iTunes XML Playlist to m3u Converter

# don't forget to excape any slashes \/
^\/Volumes\/edata\/iMusicLibrary\/.*$

# select any line that starts with "#EXTINF:" could have any number after colon, and ends with comma.
# examples that are removed: "#EXTINF:177,", "#EXTINF:246,", "#EXTINF:192,"
^#EXTINF:.*?,

get file name extension

let file = url.match(/?<=\.)[^.\\/:*?"<>|\r\n]+$/)
console.log(file[0])
	

remove file extension from path

filePath.replace(/\.[^/.]+$/, '')
Find and Replace string between and but starts with [[http
const regex = \[\[(http.*?)\]\]
// replace string
[link]($1)
// `$` means the output variable

find replace string between [and ](

const regex = \[(.*)\]\((.*)\)

replace string -->  $1 

HTML Input Pattern Attribute

An <input> element with type="email" that must be in the following order: characters@characters.domain (characters followed by an @ sign, followed by more characters, and then a "."

After the "." sign, add at least 2 letters from a to z:

<form action="/action_page.php">  
  <label for="email">Email:</label>  
  <input type="email" id="email" name="email"  
  pattern="[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,}$">  
  <input type="submit">  
</form>

Find multi line ledger and replace account and category

example

find

2023/03/16 Retirement Account
    Assets:Checking:Chase                        -10 USD
    Assets:Investments:Unknown

and replaces it with

2023/03/16 Retirement Account
    Assets:Checking:Chase                        -10 USD
    Assets:Investments:Retirement Account

Regex

// regex find
^(\d{4}/\d{2}/\d{2} Retirement Account.*\n.*Assets:Checking:Chase.*\n)    Assets:Investments:Unknown

// replace with
$1    Assets:Investments:Retirement Account

Credits