Installations
npm install perf-regexes
Developer
aMarCruz
Developer Guide
Module System
CommonJS, UMD
Min. Node Version
>=6.14
Typescript Support
Yes
Node Version
8.11.2
NPM Version
5.6.0
Statistics
12 Commits
3 Watching
1 Branches
1 Contributors
Updated on 24 Dec 2018
Bundle Size
1.11 kB
Minified
689.00 B
Minified + Gzipped
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
4,660,418
Last day
4.2%
12,919
Compared to previous day
Last week
8.2%
89,753
Compared to previous week
Last month
163.1%
308,926
Compared to previous month
Last year
33.8%
1,484,479
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
perf-regexes
Optimized and powerful regexes for JavaScript
Breaking Changes
In ES5, matching literal regexes with other regex in medium complexity code is highly risky.
In ES6 it is practically impossible.
For this reason, as of v1.0 JS_REGEX_P
is deprecated and will be removed in the next minor version.
JS_REGEX
will be maintained, but its use should be limited to complement other utilities, such as skip-regex, which uses a customized version of JS_REGEX
to identify regular expresions reliably.
The minimum supported version of NodeJS now is 6.14 (oldest maintained LTS version w/fixes).
Install
1npm install perf-regexes --save 2# or 3yarn add perf-regexes
In the browser, this loads perf-regexes in the global R
object:
1<script src="https://unpkg.com/perf-regexes/index.min.js"></script>
Included Regexes
All of these regexes recognize Win/Mac/Unix line-endings and are ready to be used, but you can customize them using the RegExp
constructor and the source
property of the desired regex.
HTML:
Name | Flags | Matches |
---|---|---|
HTML_CMNT | g | Valid HTML comments, according to the SGML standard. |
JavaScript:
Name | Flags | Matches |
---|---|---|
JS_MLCMNT | g | Multiline JS comment, with support for embedded '/*' sequences. |
JS_SLCMNT | g | Single-line JS comments, not including its line-ending. |
JS_DQSTR | g | Double quoted JS string, with support for escaped quotes and line-endings. |
JS_SQSTR | g | Single quoted JS string, with support for escaped quotes and line-endings. |
JS_STRING | g | Combines JS_SQSTR and JS_DQSTR to match single or double quoted strings. |
JS_REGEX | g | Regex. Note: The result must be validated. |
JS_REGEX_P | g | Deprecated, do not use it. |
Selection of lines:
Name | Flags | Matches |
---|---|---|
EMPTY_LINES | gm | Empty line or line with only whitespace within, including its line-ending, if it has one. |
NON_EMPTY_LINES | gm | Line with at least one non-whitespace character, including its line-ending, if it has one. |
TRAILING_WS | gm | The trailing whitespace of a line, without including its line-ending. |
OPT_WS_EOL | g | Zero or more blank characters followed by a line-ending, or the final blanks, if the (last) line has no line-ending. |
EOL | g | Line-ending of any type |
NOTE
Because the 'g'
flag, always set lastIndex
or clone the regex before using it with the exec
method.
Example
Using only one regex, this simple example will...
- Remove trailing whitespace of each line.
- Remove the empty lines.
- Normalize the line-endings to unix style.
1const R = require('perf-regexes') 2 3const cleaner = (text) => text.split(R.OPT_WS_EOL).filter(Boolean).join('\n') 4 5console.dir(cleaner(' \r\r\n\nAA\t\t\t\r\n\rBB\nCC \rDD ')) 6// ⇒ 'AA\nBB\nCC\nDD'
Use the previous function to cleanup HTML text:
1const htmlCleaner = (html) => cleaner(html.replace(R.HTML_CMNT, '')) 2 3console.dir(htmlCleaner( 4 '\r<!--header--><h1>A</h1>\r<div>B<br>\r\nC</div> <!--end-->\n')) 5// ⇒ '<h1>A</h1>\n<div>B<br>\nC</div>'
Line-endings Normalization
1const R = require('perf-regexes') 2 3const normalize = (text) => text.split(R.EOL).join('\n') 4 5console.dir(normalize('\rAA\r\r\nBB\r\nCC \nDD\r')) 6// ⇒ '\nAA\n\nBB\nCC \nDD\n'
Double-quoted to single-quoted strings
1const toSingleQuotes = (text) => text.replace(R.JS_STRING, (str) => { 2 return str[0] === '"' 3 ? `'${str.slice(1, -1).replace(/'/g, "\\'")}'` 4 : str 5}) 6 7console.log(toSingleQuotes(`"A's" 'B' "C"`)) 8// ⇒ 'A\'s' 'B' 'C'
Matching Regexes
With the arrival of ES6TL and new keywords, finding literal regexes with another regex is not viable, you need a JS parser such as acorn or a specialized one such as skip-regex to do it correctly.
This is a very basic example that uses skip-regex:
1import R from 'perf-regexes' 2import skipRegex from 'skip-regex' 3 4/** 5 * Source to match quoted string, comments, and slashes. 6 * Captures en $1 the slash 7 */ 8const reStr = `${R.JS_STRING.source}|${R.JS_MLCMNT.source}|${R.JS_SLCMNT.source}|(/)` 9 10/** 11 * Search regexes in `code` and display the result to the console. 12 */ 13const searchRegexes = (code) => { 14 15 // Creating `re` here keeps its lastIndex private 16 const re = RegExp(reStr, 'g') 17 let match = re.exec(code) 18 19 while (match) { 20 if (match[1]) { 21 const start = match.index 22 const end = skipRegex(code, start) 23 24 // skipRegex returns start+1 if this is not a regex 25 if (end > start + 1) { 26 console.log(`Found "${code.slice(start, end)}" at ${start}`) 27 } 28 re.lastIndex = end 29 } 30 match = re.exec(code) 31 } 32} 33 34const code = ` 35const A = 2 36const s = '/A/' // must not find /A/ 37 38const re1 = /A/g // regex 39re1.lastIndex = 2 /A/ 1 // must not find /A/ 40 41/* /B/ // must not find /B/ 42*/ 43const re2 = /B/g // regex 44re1.exec(s || "/B/") // must not find /B/ 45` 46 47searchRegexes(code) 48// output: 49// Found "/A/g" at 74 50// Found "/B/b" at 210
The previous code does not support ES6TL, but it works quite well on ES5 files and is very fast.
For a more complete example of using perf-regexes, see js-cleanup, an advanced utility with support for ES6 that trims trailing spaces, compacts empty lines, normalizes line-endings, and removes comments conditionally.
ES6 Template Literals
ES6TLs are too complex to be identified by one single regex, do not even try.
Support my Work
I'm a full-stack developer with more than 20 year of experience and I try to share most of my work for free and help others, but this takes a significant amount of time and effort so, if you like my work, please consider...
Of course, feedback, PRs, and stars are also welcome 🙃
Thanks for your support!
License
The MIT License (MIT)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
Found 0/12 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
58 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-cwfw-4gq5-mrqx
- Warn: Project is vulnerable to: GHSA-g95f-p29q-9xw4
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-j4f2-536g-r55m
- Warn: Project is vulnerable to: GHSA-r7qp-cfhv-p84w
- Warn: Project is vulnerable to: GHSA-3gx7-xhv7-5mx3
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-6x33-pw7p-hmpq
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-2pr6-76vf-7546
- Warn: Project is vulnerable to: GHSA-8j8c-7jfh-h6hx
- Warn: Project is vulnerable to: GHSA-7x7c-qm48-pq9c
- Warn: Project is vulnerable to: GHSA-rc3x-jf5g-xvc5
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-jf85-cpcp-j695
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-82v2-mx6x-wq7q
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m / GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-h9rv-jmmf-4pgx
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-fxwf-4rqh-v8g3
- Warn: Project is vulnerable to: GHSA-25hc-qcg6-38wj
- Warn: Project is vulnerable to: GHSA-xfhh-g9f5-x4m4
- Warn: Project is vulnerable to: GHSA-qm95-pgcg-qqfq
- Warn: Project is vulnerable to: GHSA-cqmj-92xf-r6r9
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-mgfv-m47x-4wqp
- Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q
- Warn: Project is vulnerable to: GHSA-72mh-269x-7mh5
- Warn: Project is vulnerable to: GHSA-h4j5-c7cj-74xg
Score
1.7
/10
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to perf-regexes
ua-regexes-lite
A lite useragent regexes collection.
eslint-plugin-react-perf
Performance-minded React linting rules for ESLint
regex
Regex template tag with extended syntax, context-aware interpolation, and always-on best practices
perf-marks
The simplest and lightweight solution for User Timing API in Javascript.