Index

Javascript

check if variable exists
if(typeof selenium === 'undefined') {
if(typeof selenium !== 'undefined') {
get the type of a variable
typeof [variable]
convert variable to a number
Number([variable])

regex

create regex
var regex = /.+/;
var regex = new RegExp('.+');
create regex with flags
var regex = new RegExp('[0-9]+', 'g');
var regex = /[0-9]+/g;
check if string matches regex
var success = string.match(regex);
find match
var myArray = regex.exec('[input]');
find index of first match
string.search(regex);
regex replace
string.replace(regex, '[replace]');
watch out: literal string replace
string.replace('find literal string', '[replacement]');

regex syntax

available flags
i = case insensitive
g = general search (all matches)
m = multiline mode
s = dot '.' matches '\n'
u = unicode support
y = sticky (exact position match)
escape sequences
\\ = backslash
\. = literal dot '.'
\t = tab
\r = carriage return
\n = newline
\u[code] = unicode code point in hex
meta characters
. = any character
^ = begin of input
$ = end of input
\b = word boundary
\B = not a word boundary
\d = digit
\D = not a digit
\s = whitespace
\S = not whitespace
\w = word
\W = not a word character
repetition
? = optional character, greedy
?? = optional character, lazy
* = zero or more characters, greedy
*? = zero or more characters, lazy
+ = one or more characters, greedy
+? = one or more characters, lazy
{N} = exactly N characters
{N,} = N or more characters, greedy
{N,}? = N or more characters, lazy
{N,M} = N to M characters, greedy
{N,M}? = N to M characters, lazy

examples

all input
/.+/
everything up until the last slash in string
/.*[/]/
documentation
https://www.w3schools.com/jsref/jsref_obj_regexp.asp
https://javascript.info/regular-expressions

arrays

initialize empty array
var a = [];
initialize array with values
var a = [ 0, 1, 2 ]

functions

defining a function
function foo() {
    alert('foo');
}

objects

// define object
var person = {
    variableA: "A",
    variableB: "B",
};
object with method
var person = {
    variableA: "A",
    nethodName : function() {
        return "hello " + this.variableA;
    }
};

control flow

iterate array
for(i=0; i<parent.length; i++) {
	Console.log(parent[i]);
}

dom

lookup in dom using css selector
var e = document.querySelectorAll("[selector]");
change window title
document.title = 'New Title';
go to url
window.location.href = 'http://www.example.com'
get full url of current page
window.location.href
get domain name only
window.location.hostname returns the domain name of the web host
get path only
window.location.pathname;
get protocol only (http|https)
window.location.protocol

Threads

invoke with a delay
setTimeout(function() { alert('foo'); }, [millis]);
delay event until user stops typing
function delay(millis, callback) {
  var timer = 0;
  return function() {
    var context = this, args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      callback.apply(context, args);
    }, millis);
  };
}
example usage
document.getElementById('foo').onkeypress(delay(500, function (e) {
	alert('foo');
}));

key codes for events

https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
http://rmhh.co.uk/ascii.html
http://keycode.info/