js function test

Published on Tue Mar 01 2022

Additional matching regexes for
js function test

Function Declaration / Expression

matches both function declarations and expressions in javascript. ex. function name () {...} || function () {}

Arrow function to Normal function

Convert Arrow Function to Normal Function

Title Casing

for use in function to title case a string; function cap(str) { return str.replace(/\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); }); }

extract JavaScript function name

Extracts the name of the outer function. Useful to get the name of a function reference using fn.toString().


match function names in defun constructs

Replace text domain in WP i18n

This will find all instances where the WP i18n function `__()` is used, and change the text domain to something else. This will recognize instances of the function use as separate, even if they appear in the same function


Local variable and function parameter names should comply with a naming convention

Wikipedia anchor

Match Wikipedia anchor dot-instead-of-percent encoding string.replace(/((?:\.[a-f0-9]{2}){2})/ig, function(match) { return decodeURIComponent(match.replace(/\./g, '%')) });

Strip multi-line (star) comments from JavaScript, while leaving any CDATA sections intact.

This regular expression removes multi-line (star) comments from JavaScript, while leaving any CDATA sections intact. Use the global (g) modifier to match them all (if you plan to use PHP's preg_replace, the g modifier is not necessary), and use the (s) modifier to make dots match newlines (this will be required in for using with PHP's preg_replace function). This regular expression does not exhaust all possibilities (such as a space between the opening or closing star comment and the CDATA tag), but it functions if the style guidelines are of the more popularized variety.

Svelte: CSS class attribute (Svelte or HTML friendly), class:directive, TailwindCSS classes, and @apply.

# SvelteComponent Class Directive Matching Also supports TailwindCSS-style `@apply` directive for the class value. ## `class:directive={boolean}` Svelte allows us to provide a "class directive", or a class name in the form of `class:NAME={boolean}`. If we place a boolean (or function that returns a boolean) as its value, it will add the class "NAME" to the component when it returns true, or omit it when false. This is extremely useful for navigation components, and I use it all the time to highlight active links in my UX/UI development process. ```svelte <NavBar> <Link href="#abc" class:active={page === 'abc'}>ABC</Link> <Link href="#xyz" class:active={page === 'xyz'}>XYZ</Link> </NavBar> ``` Flexible matching for the class attribute on both **Svelte Components** and **HTML Elements**.