Skip to content
Created: November 2, 2016

1. Date processing ​

1. Is the inspection date valid? ​

This method is used to check whether the given date is valid:

javascript
const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf());

isDateValid("December 17, 1995 03:24:00");  // true

2. Calculate the interval between two dates ​

This method is used to calculate the time between two dates:

javascript
const dayDif = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000)

dayDif(new Date("2021-11-3"), new Date("2022-2-1"))  // 90

There are still 90 days until Chinese New Year~

3. Find the day of the year a date falls on ​

Calculate the ordinal from local calendar fields, using UTC date differences to avoid daylight-saving shifts.

javascript
const dayOfYear = date => Math.floor(
  (Date.UTC(date.getFullYear(), date.getMonth(), date.getDate()) -
   Date.UTC(date.getFullYear(), 0, 0)) / 86400000
);

dayOfYear(new Date());   // 307

More than 300 days have passed in 2021~

4. time formatting ​

This method can be used to convert time into hour:minutes:seconds format:

javascript
const timeFromDate = date => date.toTimeString().slice(0, 8);

timeFromDate(new Date(2021, 11, 2, 12, 30, 0));  // 12:30:00
timeFromDate(new Date());  // 返回当前时间 09:00:00

2. String processing ​

1. Capitalize the first letter of a string ​

This method is used to capitalize the first letter of an English string:

javascript
const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)

capitalize("hello world")  // Hello world

2. flip string ​

This method is used to flip a string and return the flipped string:

javascript
const reverse = str => str.split('').reverse().join('');

reverse('hello world');   // 'dlrow olleh'

3. random string ​

This method is used to generate a random string:

javascript
const randomString = () => Math.random().toString(36).slice(2);

randomString();

4. truncate string ​

This method truncates a string at a specified length:

javascript
const truncateString = (string, length) => string.length < length ? string : `${string.slice(0, length - 3)}...`;

truncateString('Hi, I should be truncated because I am too loooong!', 36)   // 'Hi, I should be truncated because...'

5. Remove HTML from string ​

This method is used to remove HTML elements from a string:

javascript
const stripHtml = html => (new DOMParser().parseFromString(html, 'text/html')).body.textContent || '';

3. Array processing ​

1. Remove duplicates from array ​

This method is used to remove duplicates from an array:

javascript
const removeDuplicates = (arr) => [...new Set(arr)];

console.log(removeDuplicates([1, 2, 2, 3, 3, 4, 4, 5, 5, 6]));

2. Determine whether the array is empty ​

This checks whether an array is nonempty; non-array inputs return false.

javascript
const isNotEmpty = arr => Array.isArray(arr) && arr.length > 0;

isNotEmpty([1, 2, 3]);  // true

3. Merge two arrays ​

You can use the following two methods to merge two arrays:

javascript
const merge = (a, b) => a.concat(b);

const mergeWithSpread = (a, b) => [...a, ...b];

4. Digital operations ​

1. Determine whether a number is odd or even ​

This method is used to determine whether a number is odd or even:

javascript
const isEven = num => num % 2 === 0;

isEven(996);

2. Get the average of a set of numbers ​

javascript
const average = (...args) => args.reduce((a, b) => a + b) / args.length;

average(1, 2, 3, 4, 5);   // 3

3. Get a random integer between two integers ​

This method is used to get a random integer between two integers

javascript
const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

random(1, 50);

4. Round to specified digits ​

This method is used to round a number to the specified number of places:

javascript
const round = (n, d) => Number(Math.round(n + "e" + d) + "e-" + d)

round(1.005, 2) //1.01
round(1.555, 2) //1.56

5. Color operation ​

1. Convert RGB to sixteen mechanism ​

This method can convert an RGB color value into a hexadecimal value:

javascript
const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);

rgbToHex(255, 255, 255);  // '#ffffff'

2. Get random hex color ​

This method is used to obtain a random hexadecimal color value:

javascript
const randomHex = () => "#" + Math.floor(Math.random() * 0x1000000).toString(16).padStart(6, "0");

randomHex();

6. Browser operation ​

1. Copy content to clipboard ​

This method uses navigator.clipboard.writeText to copy text to the clipboard:

javascript
const copyToClipboard = (text) => navigator.clipboard.writeText(text);

copyToClipboard("Hello World");

2. Clear all cookies ​

This example only attempts to delete script-visible cookies matching the root path; it cannot delete HttpOnly cookies or every path/domain variant.

javascript
const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`));

3. Get selected text ​

This method gets the text selected by the user through the built-in getSelection property:

javascript
const getSelectedText = () => window.getSelection().toString();

getSelectedText();

4. Check if it is dark mode ​

This method is used to detect whether the current environment is in dark mode. It is a Boolean value:

javascript
const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches

console.log(isDarkMode)

5. Scroll to top of page ​

This method is used to return to the top of the page:

javascript
const goToTop = () => window.scrollTo(0, 0);

goToTop();

6. Determine whether the current tab page is active ​

This method is used to detect whether the current tab has been activated:

javascript
const isTabInView = () => !document.hidden;

7. Determine whether the current device is an Apple device ​

This method is used to detect whether the current device is an Apple device:

javascript
const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform);

isAppleDevice();

8. Whether to scroll to the bottom of the page ​

This method is used to determine whether the page has reached the bottom:

javascript
const scrolledToBottom = () => document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight;

9. Redirect to a URL ​

This method is used to redirect to a new URL:

javascript
const redirect = url => location.href = url

redirect("https://www.google.com/")

10. Open browser print box ​

This method is used to open the browser's print box:

javascript
const showPrintDialog = () => window.print()

7. Other operations ​

1. random boolean ​

This method can return a random Boolean value. Use Math.random() to get a random number from 0 to 1. Compared with 0.5, there is a half probability of getting a true or false value.

javascript
const randomBoolean = () => Math.random() >= 0.5;

randomBoolean();

2. variable exchange ​

You can use the following form to swap the values of two variables without using a third variable:

javascript
[foo, bar] = [bar, foo];

3. Get the type of variable ​

This method is used to get the type of a variable:

javascript
const trueTypeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();

trueTypeOf('');     // string
trueTypeOf(0);      // number
trueTypeOf();       // undefined
trueTypeOf(null);   // null
trueTypeOf({});     // object
trueTypeOf([]);     // array
trueTypeOf(0);      // number
trueTypeOf(() => {});  // function

4. Conversion between Fahrenheit and Celsius ​

This method is used to convert between Celsius and Fahrenheit:

javascript
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;

celsiusToFahrenheit(15);    // 59
celsiusToFahrenheit(0);     // 32
celsiusToFahrenheit(-20);   // -4
fahrenheitToCelsius(59);    // 15
fahrenheitToCelsius(32);    // 0

5. Check if the object is empty ​

This method is used to detect whether a JavaScript object is empty:

javascript
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;

Regular basic syntax ​

^ 匹配一个输入或一行的开头,/^a/匹配"an A",而不匹配"An a" 
$ 匹配一个输入或一行的结尾,/a$/匹配"An a",而不匹配"an A" 
* 匹配前面元字符0次或多次,/ba*/将匹配b,ba,baa,baaa 
+ 匹配前面的表达式 1 次或多次,如 /ba+/ 匹配 ba、baa,不匹配单独的 b。
? 匹配前面的表达式 0 次或 1 次,如 /ba?/ 匹配 b、ba;紧跟量词时表示非贪婪匹配。
(x) 是捕获组;反向引用如 /([ab])\\1/,字面括号应转义为 \\( 与 \\)。
x|y 匹配x或y 
{n} 精确匹配n次 
{n,} 匹配n次以上 
{n,m} 匹配n-m次 
[xyz] 字符集(character set),匹配这个集合中的任一一个字符(或元字符) 
[^xyz] 不匹配这个集合中的任何一个字符 
[a-z] 字符范围。匹配指定范围内的任意字符。例如,“[a-z]”可以匹配“a”到“z”范围内的任意小写字母字符。
[^a-z] 负值字符范围。匹配任何不在指定范围内的任意字符。例如,“[^a-z]”可以匹配任何不在“a”到“z”范围内的任意字符。
. 匹配除“\n”之外的任何单个字符。要匹配包括“\n”在内的任何字符,请使用像“(.|\n)”的模式。
[\b] 匹配一个退格符 
\b 匹配一个单词的边界 
\B 匹配一个单词的非边界 
\cX 这儿,X是一个控制符,/\cM/匹配Ctrl-M 
\d 匹配一个字数字符,/\d/ = /[0-9]/ 
\D 匹配一个非字数字符,/\D/ = /[^0-9]/ 
\f 匹配一个换页符。等价于\x0c和\cL。
\n 匹配一个换行符 等价于\x0a和\cJ。
\r 匹配一个回车符 等价于\x0d和\cM。
\s 匹配一个空白字符,包括\n,\r,\f,\t,\v等 。等价于[ \f\n\r\t\v]。
\S 匹配一个非空白字符,等于/[^\n\f\r\t\v]/ 。[^ \f\n\r\t\v]。
\t 匹配一个制表符 。等价于\x09和\cI。
\v 匹配一个垂直制表符 等价于\x0b和\cK。
\\w 匹配 ASCII 字母、数字和下划线,基本等价于 [A-Za-z0-9_]。
\\W 匹配不属于 \\w 的字符,基本等价于 [^A-Za-z0-9_]。
\xn 匹配n,其中n为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如,“\x41”匹配“A”。“\x041”则等价于“\x04&1”。正则表达式中可以使用ASCII编码。
\num 匹配num,其中num是一个正整数。对所获取的匹配的引用。例如,“(.)\1”匹配两个连续的相同字符。
\n 标识一个八进制转义值或一个向后引用。如果\n之前至少n个获取的子表达式,则n为向后引用。否则,如果n为八进制数字(0-7),则n为一个八进制转义值。
\nm 标识一个八进制转义值或一个向后引用。如果\nm之前至少有nm个获得子表达式,则nm为向后引用。如果\nm之前至少有n个获取,则n为一个后跟文字m的向后引用。如果前面的条件都不满足,若n和m均为八进制数字(0-7),则\nm将匹配八进制转义值nm。
\nml 如果n为八进制数字(0-3),且m和l均为八进制数字(0-7),则匹配八进制转义值nml。
\un 匹配n,其中n是一个用四个十六进制数字表示的Unicode字符。例如,\u00A9匹配版权符号(©)。

The difference between .* and .*? is that the former is a greedy matching mode and the latter is a non-greedy matching mode.The same goes for .+ and .+?


(x)	
像下面的例子展示的那样,它会匹配 'x' 并且记住匹配项。其中括号被称为捕获括号。

模式 /(foo) (bar) \1 \2/ 中的 '(foo)' 和 '(bar)' 匹配并记住字符串 "foo bar foo bar" 中前两个单词。模式中的 \1 和 \2 表示第一个和第二个被捕获括号匹配的子字符串,即 foo 和 bar,匹配了原字符串中的后两个单词。注意 \1、\2、...、\n 是用在正则表达式的匹配环节,详情可以参阅后文的 \n 条目。而在正则表达式的替换环节,则要使用像 $1、$2、...、$n 这样的语法,例如,'bar foo'.replace(/(...) (...)/, '$2 $1')。$& 表示整个用于匹配的原字符串。
---------------------------------------
(?:x)	
匹配 'x' 但是不记住匹配项。这种括号叫作非捕获括号,使得你能够定义与正则表达式运算符一起使用的子表达式。看看这个例子 /(?:foo){1,2}/。如果表达式是 /foo{1,2}/,{1,2} 将只应用于 'foo' 的最后一个字符 'o'。如果使用非捕获括号,则 {1,2} 会应用于整个 'foo' 单词。更多信息,可以参阅下文的 Using parentheses 条目.
---------------------------------------
x(?=y)	
匹配'x'仅仅当'x'后面跟着'y'.这种叫做先行断言。

例如,/Jack(?=Sprat)/会匹配到'Jack'仅仅当它后面跟着'Sprat'。/Jack(?=Sprat|Frost)/匹配‘Jack’仅仅当它后面跟着'Sprat'或者是‘Frost’。但是‘Sprat’和‘Frost’都不是匹配结果的一部分。
---------------------------------------
(?<=y)x	
匹配'x'仅仅当'x'前面是'y'.这种叫做后行断言。

例如,/(?<=Jack)Sprat/会匹配到' Sprat '仅仅当它前面是' Jack '。/(?<=Jack|Tom)Sprat/匹配‘ Sprat ’仅仅当它前面是'Jack'或者是‘Tom’。但是‘Jack’和‘Tom’都不是匹配结果的一部分。
---------------------------------------
x(?!y)	
仅仅当'x'后面不跟着'y'时匹配'x',这被称为正向否定查找。

例如,仅仅当这个数字后面没有跟小数点的时候,/\d+(?!\.)/ 匹配一个数字。正则表达式/\d+(?!\.)/.exec("3.141")匹配‘141’而不是‘3.141’
---------------------------------------
(?<!y)x	
仅仅当'x'前面不是'y'时匹配'x',这被称为反向否定查找。

例如, 仅仅当这个数字前面没有负号的时候,/(?<!-)\d+/ 匹配一个数字。
/(?<!-)\d+/.exec('3') 匹配到 "3".
/(?<!-)\d+/.exec('-3') 因为这个数字前有负号,所以没有匹配到。
---------------------------------------
x|y	
匹配‘x’或者‘y’。

例如,/green|red/匹配“green apple”中的‘green’和“red apple”中的‘red’

Application summary ​

Verify whether consecutive numbers are repeated ​

js
"bcae1958fiefadf1958fef".match(/^.*?(\d{4})(?:(?!\1).)*\1/)

Verify if there are duplicate characters ​

js

"bcae1958fiqpdljwrxy".match(/^.*?(\w)(?:(?!\1).)*\1/)

IP address regularity ​

js
/^((([1-9]?\d)|(1\d\d)|(2[0-4]\d)|(25[0-5]))\.){3}(([1-9]?\d)|(1\d\d)|(2[0-4]\d)|(25[0-5]))$/.test('0.255.255.0')

12 digits, 6 consecutive identical digits cannot appear in it ​

js

'123777890987'.match(/^(?:([0-9])(?!\1{5})){12}$/)

10 non-repeating letters within A-Z ​

Method one:

js
/^(?:([A-Z])(?!.*?\1)){10}$/.test("ABCDEFGHIJ");

Method two:

js
'asdfghjklp'.match(/^(?:([a-z])(?=((?!\1).)*$)){10}$/)

Thousands separator (js implementation) ​

js

function thousandBitSeparator(num) {
    if (!Number.isFinite(num)) throw new TypeError('Expected a finite number');
    const [integer, fraction] = String(num).split('.');
    const grouped = integer.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    return fraction === undefined ? grouped : grouped + '.' + fraction;
}
console.log(thousandBitSeparator(-1234567.9012));
js

var a = 1;
function b(){
    var a=2;
    function c(){
        console.log(a)
    }
    return c;
}

b()();

Author: CUGGZ Link: https://juejin.cn/post/7025771605422768159