I'm using react-maskedinput and for mask it accept regular expression . I'm getting mask from backend . Sometimes mask is empty and I need to set default mask. If it's empty I want to allow user type any 40 symbols.
export function getMaskForInput(mask) {
const newMask = mask.toString().replace(/`9/g, 'q').replace(/9/g, '1').replace(/q/g, '`9');
const emptyMask = '/\W{40}/g'; // probably not correct expression
if (mask) {
return newMask.replace(/`/g, '\\').replace('$', '');
}
console.log('empty mask');
return emptyMask;
}
const mask = getMaskForInput(maskEdit)
<MaskedInput
mask={mask}
type="tel"
id="account"
autoComplete="off"
maxLength={40}
placeholder="№ лицевого счета"
placeholderChar=" "
onChange={(e) => this.handleOnAccountChange(e.target.value.trim())}
value={this.state.account}
formatCharacters={{
W: {
validate(char) { return /[\wа-яА-Я \-0-9.@]/.test(char); },
},
w: {
validate(char) { return /[\wа-яА-Я \-0-9.@]/.test(char); },
}
}}
/>
But when I return 'emptyMask' it shows my actual regex for mask . Where I'm making mistake?

'/\W{40}/g'is a string literal containing a regex literal, it is wrong. You need/\W{40}/g, without quotes, if you need to set a regex.maskof typeregexpsupplied toMaskedInput, expectedstring."