Skip to content

Commit 9316d66

Browse files
committed
Add: replaceCharOutsideQuotes util function
1 parent 29acc98 commit 9316d66

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default function replaceCharOutsideQuotes(str: string, target: string, replace: string) {
2+
let result = ''
3+
let inSingle = false
4+
let inDouble = false
5+
for (let i = 0; i < str.length; i++) {
6+
const char = str[i]
7+
const prev = str[i - 1]
8+
if (char === '\'' && prev !== '\\' && !inDouble) inSingle = !inSingle
9+
else if (char === '"' && prev !== '\\' && !inSingle) inDouble = !inDouble
10+
if (char === target && !inSingle && !inDouble) {
11+
result += replace
12+
} else {
13+
result += char
14+
}
15+
}
16+
return result
17+
}

0 commit comments

Comments
 (0)