Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/TextInputMask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ function detectCharacter(mask: string): string {
return c || ''
}

function escapeForRegExp(value: string): string {
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dot is the only special character that is used here for splitCharacter. I added all special characters here because maybe this can be abstracted to util in the future and used in other places. If you prefer to have only ., I can change this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

function TextInputWithMask(
{
onChangeText,
Expand Down Expand Up @@ -40,7 +44,7 @@ function TextInputWithMask(
)
const replaceValue = format.match(/\W/)
const replace = `$1${splitCharacter}$2${splitCharacter}$3$4`.replace(
new RegExp(splitCharacter, 'g'),
new RegExp(escapeForRegExp(splitCharacter), 'g'),
(replaceValue ?? '') as string
)

Expand Down