Skip to content

Commit

Permalink
fix replace all with special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Huỳnh Đức Khoản authored and mayswind committed Dec 20, 2024
1 parent 263bf08 commit d0a5c93
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ export function getObjectOwnFieldCount(object) {
}

export function replaceAll(value, originalValue, targetValue) {
return value.replaceAll(new RegExp(originalValue, 'g'), targetValue);
// Escape special characters in originalValue to safely use it in a regex pattern.
// This ensures that characters like . (dot), * (asterisk), +, ?, etc. are treated literally,
// rather than as special regex symbols.
const escapedOriginalValue = originalValue.replace(/([.*+?^=!:${}()|\-/\\])/g, '\\$1');

return value.replaceAll(new RegExp(escapedOriginalValue, 'g'), targetValue);
}

export function removeAll(value, originalValue) {
Expand Down

0 comments on commit d0a5c93

Please sign in to comment.