Remove new lines in quotations from csv with Regex

Regex Expression to remove new lines which are quoted in csv.

Csv is a weird format. It has never been officially documented by Microsoft and still there is no hard standards for any library to follow. Especially around quoting strings and new lines (outside of unquoted ones which mark new row of a datatable)

/(?<=\"[^\"]*)[\r\n]+(?=[^\"]*\"\,)/gi

It should work fine for both windows and unix new lines quoted between regular quotation mark ( " ).

If you want to get rid of new lines you can use regex regex replace with above expression, and replace with \t - which will be tab.

Toggle window snippet (show / hide / bring to front) with Autohotkey

This piece of code toggles any window. It will * RESTORE if window is minimized, * Bring to the top so window is foremost if it is behind other windows, * Minimize if window is active and on top,

Quick toggle any window.

This piece of code toggles any window [Autohotkey].

It will

  • RESTORE if window is minimized
  • Bring to the top so window is foremost if it is behind other windows
  • MINIMIZE if window is currently active and on top

Spotify is a being toggled in the below code, just as an example.

WindowTitle = ahk_exe Spotify.exe ;just as an example 

if (ID := WinExist(WindowTitle)){
WinGet, state, MinMax, %WindowTitle%

if (state = -1) {
WinRestore %WindowTitle%
WinActivate
} Else {
WinGetPos , X, Y, , , %WindowTitle%
If ( WinActive(WindowTitle))
WinMinimize, %WindowTitle%
Else
WinActivate, %WindowTitle%
}
}

Online code utilities

List of helpful online code tools and utilities. Mirrored from github user fghber

This has been mirrored from github - fghber's gist

Web IDE

ideone Ideone is an online compiler and debugging tool which allows youto compile source code and execute it online in more than 60 programming languages and share links to the code.
repl.it Like ideaone, can also install missing Python packages.
Onlilne GDB Online compiler and debugger forC, C++, Python, Java, PHP, Ruby, Perl, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog, and more.
Coding Ground Advanced IDEs for C/C++, C#, GO, Java, PHP, Perl, Python, Octave/MATLOAB, R, Ruby, Scala, and compiler/interpreter for many other languages and frameworks.
JDOODLE very similar to ideone but a somewhat different set of languagse/compilers.

Dedicated: JSFiddle Online JavaScript, CSS, HTML or CoffeeScript code editor. Option to add various web frameworks. .NET Fiddle Online compiler for C#, VB.NET, and F# with .NET 4.5 & Roslyn 2.0. Option to install additional nuget packages.

(Cloud workspaces: https://codenvy.com/, https://codeanywhere.com/)

RegEx

Online Tester & Code Generator

regex101 Online regex tester for PHP, PCRE, JavaScript and Python that highlights pattern and matches on the fly.
https://regexr.com/ Online regex tester with gut regex replace and explain functionality.
https://www.debuggex.com/ Can show neat expression diagrams (not active anymore).
https://regexper.com/ also shows expression diagrams.
https://myregextester.com/index.php Allows to optimize patterns (I guess this is based on this Perl's Regexp::Optimizer).

Libraries

http://regexlib.com/ Regular Expression Library. Currently has 7601 expressions from 2492 contributors (most are subpar IMHO).

Generator

txt2re: regular expression generator (with code generator for perl php python java javascript coldfusion c c++ ruby vb vbscript j# c# c++.net vb.net)

.NET Regex

http://regexstorm.net/tester
http://regexhero.net/tester/
http://www.beansoftware.com/Test-Net-Regular-Expressions/Split-String.aspx

Java Regex

http://java-regex-tester.appspot.com/

Perl

http://www.regexplanet.com/advanced/perl/index.html
http://www.regexe.com/

R

https://spannbaueradam.shinyapps.io/r_regex_tester/
http://rextester.com/l/r_online_compiler

Ruby

http://rubular.com/

htaccess tester

http://htaccess.mwl.be/

Google Analytics Regex Tester

http://www.analyticsmarket.com/freetools/regex-tester (and a general GA RE usage guide)

Regex Reverse Pattern generator

sre_yield: generate all values that can match a given regular expression, or count possible matches efficiently
Xeger: Java library for generating strings that match a specific regular expression.
PyXeger: Java library for generating strings that match a specific regular expression.

General Languages

https://www.tutorialspoint.com/codingground.htm

.NET

https://dotnetfiddle.net/

PHP

PHP Sandbox: Allows to test PHP code on many php versions.
https://3v4l.org/ allows to run over 200 different PHP versions (including HHVM, etc.) in parallel - excellent for version compatibility tests
http://c9.io An online IDE that supports many languages (free for personal use).
http://phpfiddle.org/ A PHP online IDE with some extra resources like code search (saved, external code).

Python

https://www.onlinegdb.com/online_python_debugger
EXREX is a command line tool and python module that generates all - or random - matching strings to a given regular expression and more.

Perl

http://rextester.com/l/perl_online_compiler
https://www.jdoodle.com/execute-perl-online
https://www.tutorialspoint.com/execute_perl_online.php

HTML

https://html-cleaner.com/ HTML/CSS/JS Cleaner

JSON:

Query test: http://www.jsonquerytool.com/#/JavaScript
Sort array: https://aruljohn.com/jsonsort/
Generate C# classes from JSON: http://json2csharp.com/ (Desktop tool: http://jsonclassgenerator.codeplex.com/)
ConvertJson.com FromJson(to XML, HTML, CSV, SQL, YAML), ToJson, JsonTools (Lint, Viewer, Format, Path Analyzer)

SQL

http://sqlfiddle.com/ (MySQL, Oracle, PosgreSQL, SQLite, MS SQL 2014)
https://sqltest.net/ (MySQL, Oracle)
https://sqlizer.io/

SQL Formater

https://sqlformat.org/ (online formatting service)
http://poorsql.com/ (Poor Man's SQL Formatter online, download for VS, SMSS, Np++, etc.).

Utilities

Code beautifier & Syntax Highlither

https://codebeautify.org/
https://tohtml.com/ Syntax Highlither for varius languages (useful for HTML & RTF text)

Character Set Conversion

http://www.fileformat.info/convert/text/charset.htm

Other

Arul's network and other technical utilities: View your IP, country, ISP name, OS, browser/UA.
Convert IP address to hostname and vice versa, text case convert, UNIX timestamp to date, hash generator
https://aruljohn.com/

Record and share Linux/Mac terminal sessions: https://asciinema.org/

Vanilla JS XPath query function

Snippet with function making DOM XPath queries easy in the browser.

Method 1

Using XPathEvaluator with start node and multiple results.
source

function evaluateXPath(aNode, aExpr) {
var xpe = new XPathEvaluator();
var nsResolver = xpe.createNSResolver(aNode.ownerDocument == null ?
aNode.documentElement : aNode.ownerDocument.documentElement);
var result = xpe.evaluate(aExpr, aNode, nsResolver, 0, null);
var found = [];
var res;
while (res = result.iterateNext())
found.push(res);
return found;
}

Sample query:

evaluateXPath(document, '//button[@title="Edit" or @title="Add"]')

/* Result:
[button#49431283ecf54dbaba1fcd8bf735af41.WABN.WEBN.WIMO.WNTN.WO5M]
0: button#49431283ecf54dbaba1fcd8bf735af41.WABN.WEBN.WIMO.WNTN.WO5M
length: 1
__proto__: Array(0)
/*

Method 2

Single node, always against document using XPathEvaluator
source

function getElementByXPath(xpath) {
return new XPathEvaluator()
.createExpression(xpath)
.evaluate(document, XPathResult.FIRST_ORDERED_NODE_TYPE)
.singleNodeValue
}

console.log( getElementByXPath("//html[1]/body[1]/div[1]") );

Method 3

Single node, against document using document.evaluate

function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

console.log( getElementByXpath("//html[1]/body[1]/div[1]") );function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

console.log( getElementByXpath("//html[1]/body[1]/div[1]") );

XPath cheatsheet

Very handy cheatsheet covering xpath concepts and comparing it to css selectors equivalents when possible

This has been copied from here: https://devhints.io/xpath

Testing

Xpath test bed

Test queries in the Xpath test bed:

Xpath test bed (whitebeam.org)

Browser console

$x("//div")

Works in Firefox and Chrome.

Selectors

Descendant selectors

CSSXpath?
h1//h1?
div p//div//p?
ul > li//ul/li?
ul > li > a//ul/li/a
div > *//div/*
----------
:root/?
:root > body/body

{: .xp}

Attribute selectors

CSSXpath?
#id//*[@id="id"]?
.class//*[@class="class"] ...kinda
input[type="submit"]//input[@type="submit"]
a#abc[for="xyz"]//a[@id="abc"][@for="xyz"]?
a[rel]//a[@rel]
----------
a[href^='/']//a[starts-with(@href, '/')]?
a[href$='pdf']//a[ends-with(@href, '.pdf')]
a[href*='://']//a[contains(@href, '://')]
a[rel~='help']//a[contains(@rel, 'help')] ...kinda

{: .xp}

Order selectors

CSSXpath?
ul > li:first-child//ul/li[1]?
ul > li:nth-child(2)//ul/li[2]
ul > li:last-child//ul/li[last()]
li#id:first-child//li[@id="id"][1]
a:first-child//a[1]
a:last-child//a[last()]

{: .xp}

Siblings

CSSXpath?
h1 ~ ul//h1/following-sibling::ul?
h1 + ul//h1/following-sibling::ul[1]
h1 ~ #id//h1/following-sibling::[@id="id"]

{: .xp}

jQuery

CSSXpath?
$('ul > li').parent()//ul/li/..?
$('li').closest('section')//li/ancestor-or-self::section
$('a').attr('href')//a/@href?
$('span').text()//span/text()

{: .xp}

Other things

CSSXpath?
h1:not([id])//h1[not(@id)]?
Text match//button[text()="Submit"]?
Text match (substring)//button[contains(text(),"Go")]
Arithmetic//product[@price > 2.50]
Has children//ul[*]
Has children (specific)//ul[li]
Or logic//a[@name or @href]?
Union (joins results)//a | //div?

{: .xp}

Class check

//div[contains(concat(' ',normalize-space(@class),' '),' foobar ')]

Xpath doesn't have the "check if part of space-separated list" operator, so this is the workaround (source).

Expressions

Steps and axes

AxisStepAxisStep
//ul/a[@id='link']

{: .-css-breakdown}

Prefixes

PrefixExampleWhat
////hr[@class='edge']Anywhere
././aRelative
//html/body/divRoot

{: .-headers}

Begin your expression with any of these.

Axes

AxisExampleWhat
///ul/li/aChild
////[@id="list"]//aDescendant

{: .-headers}

Separate your steps with /. Use two (//) if you don't want to select direct children.

Steps

//div
//div[@name='box']
//[@id='link']

A step may have an element name (div) and predicates ([...]). Both are optional.
They can also be these other things:

//a/text()     #=> "Go home"
//a/@href #=> "index.html"
//a/* #=> All a's child elements

Predicates

Predicates

//div[true()]
//div[@class="head"]
//div[@class="head"][@id="top"]

Restricts a nodeset only if some condition is true. They can be chained.

Operators

# Comparison
//a[@id = "xyz"]
//a[@id != "xyz"]
//a[@price > 25]
# Logic (and/or)
//div[@id="head" and position()=2]
//div[(x and y) or not(z)]

Use comparison and logic operators to make conditionals.

Using nodes

# Use them inside functions
//ul[count(li) > 2]
//ul[count(li[@class='hide']) > 0]
# This returns `<ul>` that has a `<li>` child
//ul[li]

You can use nodes inside predicates.

Indexing

//a[1]                  # first <a>
//a[last()] # last <a>
//ol/li[2] # second <li>
//ol/li[position()=2] # same as above
//ol/li[position()>1] # :not(:first-child)

Use [] with a number, or last() or position().

Chaining order

a[1][@href='/']
a[@href='/'][1]

Order is significant, these two are different.

Nesting predicates

//section[//h1[@id='hi']]

This returns <section> if it has an <h1> descendant with id='hi'.

Functions

Node functions

name()                     # //[starts-with(name(), 'h')]
text() # //button[text()="Submit"]
# //button/text()
lang(str)
namespace-uri()
count()                    # //table[count(tr)=1]
position() # //ol/li[position()=2]

Boolean functions

not(expr)                  # button[not(starts-with(text(),"Submit"))]

String functions

contains()                 # font[contains(@class,"head")]
starts-with() # font[starts-with(@class,"head")]
ends-with() # font[ends-with(@class,"head")]
concat(x,y)
substring(str, start, len)
substring-before("01/02", "/") #=> 01
substring-after("01/02", "/") #=> 02
translate()
normalize-space()
string-length()

Type conversion

string()
number()
boolean()

Axes

Using axes

//ul/li                       # ul > li
//ul/child::li # ul > li (same)
//ul/following-sibling::li # ul ~ li
//ul/descendant-or-self::li # ul li
//ul/ancestor-or-self::li # $('ul').closest('li')

Steps of an expression are separated by /, usually used to pick child nodes. That's not always true: you can specify a different "axis" with ::.

| // | ul | /child:: | li |
| Axis | Step | Axis | Step |
{: .-css-breakdown}

Child axis

# both the same
//ul/li/a
//child::ul/child::li/child::a

child:: is the default axis. This makes //a/b/c work.

# both the same
# this works because `child::li` is truthy, so the predicate succeeds
//ul[li]
//ul[child::li]
# both the same
//ul[count(li) > 2]
//ul[count(child::li) > 2]

Descendant-or-self axis

# both the same
//div//h4
//div/descendant-or-self::h4

// is short for the descendant-or-self:: axis.

# both the same
//ul//[last()]
//ul/descendant-or-self::[last()]

Other axes

AxisAbbrevNotes
ancestor
ancestor-or-self
---------
attribute@@href is short for attribute::href
childdiv is short for child::div
descendant
descendant-or-self//// is short for /descendant-or-self::node()/
namespace
---------
self.. is short for self::node()
parent.... is short for parent::node()
---------
following
following-sibling
preceding
preceding-sibling

{: .-headers}

There are other axes you can use.

Unions

//a | //span

Use | to join two expressions.

More examples

Examples

//*                 # all elements
count(//*) # count all elements
(//h1)[1]/text() # text of the first h1 heading
//li[span] # find a <li> with an <span> inside it
# ...expands to //li[child::span]
//ul/li/.. # use .. to select a parent

Find a parent

//section[h1[@id='section-name']]

Finds a <section> that directly contains h1#section-name

//section[//h1[@id='section-name']]

Finds a <section> that contains h1#section-name.
(Same as above, but uses descendant-or-self instead of child)

Closest

./ancestor-or-self::[@class="box"]

Works like jQuery's $().closest('.box').

Attributes

//item[@price > 2*@discount]

Finds <item> and check its attributes

References

{: .-one-column}

More helpful resources:

Ultimate slugify function

Ultimate slugify function

Best approach I could find if you don't want any additional libs for this transformation.

function slugify(string) {
const a = 'àáâäæãåāăąçćčđďèéêëēėęěğǵḧîïíīįìłḿñńǹňôöòóœøōõőṕŕřßśšşșťțûüùúūǘůűųẃẍÿýžźż·/_,:;'
const b = 'aaaaaaaaaacccddeeeeeeeegghiiiiiilmnnnnoooooooooprrsssssttuuuuuuuuuwxyyzzz------'
const p = new RegExp(a.split('').join('|'), 'g')

return string.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(p, c => b.charAt(a.indexOf(c))) // Replace special characters
.replace(/&/g, '-and-') // Replace & with 'and'
.replace(/[^\w\-]+/g, '') // Remove all non-word characters
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, '') // Trim - from end of text
}

Iterating through nested JavaScript object and extracting data.

How to iterate and extract data from nested JSON in case there is no structure or limit of depth.

This one really got me. Mostly because i spent 40 minutes on mindless looking for ready to take solution.
After checking dozen of lodash methods almost on random hoping it will just work i realized i had to invest some effort ;)

But once you get past the scary bit that "IT CAN GO FOREVER", and realize that recursive function is what you need - it is really trivial.

I needed to parse resulting JSON from chrome extensions API chrome.bookmarks.get and extract folder names and their ids.

Retrieved type is called BookmarkTreeNode.

It is basically simple object with some string and integer props, but one property named'children' can be an array of nested - children nodes. It is a property of object which usually represent parent folder.

So what we need to do is to check the properties of currently iterated object and first, save folder name and id to our empty array (as this was my goal in the first place) and check if among properties is another object to iterate.
And that's it.

const links = require('./links.json'); 
let folders = []; // folders will be pushed here

const checkObj = obj => {
if ('dateGroupModified' in obj) // 'dateGroupModified' is present only for folder node
folders.push({ title: obj.title, id: obj.id });
Object.keys(obj).forEach(key => {
if (typeof obj[key] == 'object') checkObj(obj[key]);
});
};

checkObj(links);