Community Patterns

1

JavaScript number/bigint literals

Created·2024-02-14 23:07
Updated·2025-06-10 00:23
Flavor·ECMAScript (JavaScript)
Should match any valid JavaScript number/bigint literal. See this StackOverflow answer by me for matching JS numbers. open in regex101 editor - example matches and unit tests\ Regulex%7C%5BoO%5D%5B0-7%5D(%3F%3A_%3F%5B0-7%5D%2B)%7C%5B0-7%5D%2B%7C%5BxX%5D%5B0-9a-fA-F%5D(%3F%3A_%3F%5B0-9a-fA-F%5D%2B))%7C(%3F%3A(%3F%3A0%7C0%5B0-9%5D%5B89%5D%5B0-9%5D%7C%5B1-9%5D(%3F%3A_%3F%5B0-9%5D%2B))(%3F%3A%5C.(%3F%3A%5B0-9%5D(%3F%3A_%3F%5B0-9%5D%2B))%3F)%3F%7C%5C.%5B0-9%5D(%3F%3A_%3F%5B0-9%5D%2B))(%3F%3A%5BeE%5D%5B%2B-%5D%3F%5B0-9%5D(%3F%3A_%3F%5B0-9%5D%2B))%3F)%7C-%3F(%3F%3A0(%3F%3A%5BbB%5D%5B01%5D(%3F%3A_%3F%5B01%5D%2B)%7C%5BoO%5D%5B0-7%5D(%3F%3A_%3F%5B0-7%5D%2B)%7C%5BxX%5D%5B0-9a-fA-F%5D(%3F%3A_%3F%5B0-9a-fA-F%5D%2B))%3F%7C%5B1-9%5D(%3F%3A_%3F%5B0-9%5D%2B)*)n)%24 "View the RegExp on jex.im/regulex") - colored graph\ Regexper - slightly smaller and less colored graph +/- Infinity and NaN _ as numerical separator between any two digits (except with leading 0s) Number leading/trailing . (decimal point) leading 0s [!] interpreted as octal when all digits are less than 8 (then . or scientific notation will error) [!] doesn't allow numerical separator (in the integer part; even with digits above 7) prefix 0b binary no . or scientific notation 0o octal no . or scientific notation 0x hexadecimal no . or scientific notation BigInt explicit via suffix n no leading 0s except after a 0o/0x/0b prefix no +, ., or scientific notation Note, Number() (parsing from a string) allows leading/trailing whitespace and scientific notation, but not `, and leading 0`s are not an implicit octal prefix, (and ofc no _BigInt suffix)
Submitted by MAZ01001
2

Detect RGB/RGBA colors in CSS

Created·2022-05-31 10:30
Flavor·PCRE2 (PHP)
JS function to convert RGB(A) to HEX color for single values: /** @param {string} color the RGB(A) color @return {string} the HEX color */ function rgba2hex(color) { const rgb = color.replace(/,\s+/g, ',').match(/^rgba?\((.\d]+)[, ]+([.\d]+)[, ]+([.\d]+)[, ]?([.\d]+)?\)$/i); let hex = (parseInt(rgb[1], 10) | 1 [.\d]+)[, ]+(?[.\d]+)[, ]+(?[.\d]+)(?:\s?[,\/]\s?(?[.\d]+%?))?\)/i', static function (array $matches) { $matches['r'] = ceil($matches['r']); $matches['g'] = ceil($matches['g']); $matches['b'] = ceil($matches['b']); if (isset($matches['a'])) { if (str_ends_with($matches['a'], '%')) { // 2.55 is 1% $matches['a'] = 2.55 * (float) substr($matches['a'], -1); } else { if ($matches['a' === '.') { $matches['a'] = '0' . $matches['a']; } $matches['a'] = 255 * (float) $matches['a']; } $matches['a'] = ceil($matches['a']); $hex = dechex(($matches['r'] << 24) | ($matches['g'] << 16) | ($matches['b'] << 8) | $matches['a']); return '#' . str_pad($hex, 8, '0', STR_PAD_LEFT); } $hex = dechex(($matches['r'] << 16) | ($matches['g'] << 8) | $matches['b']); return '#' . str_pad($hex, 6, '0', STR_PAD_LEFT); }, $css ); } Test: $css = <<<'CSS' color: rgb(34, 12, 64, 0.6); color: rgba(34, 12, 64, 0.6); color: rgba(34, 12, 64, .6); color: rgb(34 12 64 / 0.6); color: rgba(34 12 64 / 0.3); color: rgb(34.0 12 64 / 60%); color: rgba(34.6 12 64 / 30%); color: rgba(0, 255, 255); color: rgba(0, 255, 255); color: rgba(0, 255, 255, .5); color: rgba(0, 255, 255, 0.5); color: rgba(0 255 255 / 0.5); CSS; echo rgb2Hex($css); Result: color: #220c4010; color: #220c4010; color: #220c4010; color: #220c4010; color: #220c4008; color: #220c4000; color: #230c4000; color: #00ffff; color: #00ffff; color: #00ffff0d; color: #00ffff0d; color: #00ffff0d;
Submitted by WinterSilence
0

974618121112

Created·2021-11-09 19:37
Flavor·Python
``Hash: 0xcba5ee76d1b322114c4e5b1a3e00edf1065bf0346bef88ee603a8a061908db88- https://img.shields.io/azure-devops/coverage/0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3/https://tokenview.com/en/search/0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684/974618121112?color=https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112&label=%3Cimg%20alt%3D%22Azure%20DevOps%20coverage%22%20src%3D%22https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112%22%3E&logo=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2&logoColor=https%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684&style=plasticgmsUi image:: https://img.shields.io/azure-devops/coverage/0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3/https://tokenview.com/en/search/0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684/974618121112?color=https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112&label=%3Cimg%20alt%3D%22Azure%20DevOps%20coverage%22%20src%3D%22https%3A%2F%2Fimg.shields.io%2Fazure-devops%2Fcoverage%2F0xb95343413e459a0f97461812111254163ae53467855c0d73e0f1e7c5b8442fa3%2Fhttps%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684%2F974618121112%22%3E&logo=0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2&logoColor=https%3A%2F%2Ftokenview.com%2Fen%2Fsearch%2F0xdf6ac64b05e8bb7ca5c8d31ff44a00544eb039a7f467a424ae1c1e1926f19684&style=plastic :alt: Azure DevOps coverage1. -
Submitted by anonymous

Community Library Entry

1

Regular Expression
Created·2024-08-14 06:59
Flavor·PCRE2 (PHP)

/
^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
/
gm
Open regex in editor

Description

Submitted by anonymous