How to split a long regular expression into multiple lines in JavaScript?

Extending @KooiInc answer, you can avoid manually escaping every special character by using the source property of the RegExp object. Example: var urlRegex= new RegExp(” + /(?:(?:(https?|ftp):)?\/\/)/.source // protocol + /(?:([^:\n\r]+):([^@\n\r]+)@)?/.source // user:pass + /(?:(?:www\.)?([^\/\n\r]+))/.source // domain + /(\/[^?\n\r]+)?/.source // request + /(\?[^#\n\r]*)?/.source // query + /(#?[^\n\r]*)?/.source // anchor ); or if you want to … Read more

JSLint is suddenly reporting: Use the function form of “use strict”

Include ‘use strict’; as the first statement in a wrapping function, so it only affects that function. This prevents problems when concatenating scripts that aren’t strict. See Douglas Crockford’s latest blog post Strict Mode Is Coming To Town. Example from that post: (function () { ‘use strict’; // this function is strict… }()); (function () … Read more

JavaScript function order: why does it matter?

tl;dr If you’re not calling anything until everything loads, you should be fine. Edit: For an overview which also covers some ES6 declarations (let, const): https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Scope_Cheatsheet This weird behavior depends on How you define the functions and When you call them. Here’s some examples. bar(); //This won’t throw an error function bar() {} foo(); //This … Read more

tech