Skip to content

Commit 42fc173

Browse files
ghostdevv43081j
andauthored
fix: consistent and likely safer regex escaping (#1439)
Co-authored-by: James Garbutt <43081j@users.noreply.github.com>
1 parent 051f8a7 commit 42fc173

File tree

4 files changed

+16
-7
lines changed

4 files changed

+16
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@iconify-json/svg-spinners": "1.2.4",
5858
"@iconify-json/vscode-icons": "1.2.45",
5959
"@intlify/shared": "11.3.0",
60+
"@li/regexp-escape-polyfill": "jsr:0.3.4",
6061
"@lunariajs/core": "https://pkg.pr.new/lunariajs/lunaria/@lunariajs/core@904b935",
6162
"@napi-rs/canvas": "0.1.97",
6263
"@nuxt/a11y": "1.0.0-alpha.1",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shared/utils/dev-dependency.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { regExpEscape } from '@li/regexp-escape-polyfill'
2+
13
export type DevDependencySuggestionReason = 'known-package' | 'readme-hint'
24

35
export interface DevDependencySuggestion {
@@ -59,15 +61,11 @@ function isKnownDevDependencyPackage(packageName: string): boolean {
5961
)
6062
}
6163

62-
function escapeRegExp(text: string): string {
63-
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
64-
}
65-
6664
function hasReadmeDevInstallHint(packageName: string, readmeContent?: string | null): boolean {
6765
if (!readmeContent) return false
6866

69-
const escapedName = escapeRegExp(packageName)
70-
const escapedNpmName = escapeRegExp(`npm:${packageName}`)
67+
const escapedName = regExpEscape(packageName)
68+
const escapedNpmName = regExpEscape(`npm:${packageName}`)
7169
const packageSpec = `(?:${escapedName}|${escapedNpmName})(?:@[\\w.-]+)?`
7270

7371
const patterns = [

shared/utils/emoji.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { regExpEscape } from '@li/regexp-escape-polyfill'
2+
13
// copied from https://github.com/markdown-it/markdown-it-emoji/blob/master/lib/data/full.mjs
24
const emojis = {
35
'100': '💯',
@@ -1907,7 +1909,7 @@ const emojis = {
19071909

19081910
const emojisKeysRegex = new RegExp(
19091911
Object.keys(emojis)
1910-
.map(key => `:${key}:`)
1912+
.map(key => `:${regExpEscape(key)}:`)
19111913
.join('|'),
19121914
'g',
19131915
)

0 commit comments

Comments
 (0)