If you want to tackle this using regExp, it's important to remember the importance of escaping characters to ensure accurate results.
For example, searching for $5
with the expectation of finding something that includes $5
may not yield the desired outcome.
One approach is to first split by .
, preserving the character for regex purposes. Then, for each split, utilize an escape method like -> Escape string for use in Javascript regex before rejoining the .
Here's an illustration:
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
function isMatch(value, search) {
return new RegExp(
'^'+search.split('.')
.map(escapeRegExp)
.join('.')
).test(value);
}
console.log(isMatch('Ban d','.an.d'));
console.log(isMatch('Cost $5', '.....$5'));
console.log(isMatch('xBan d','.an.d'));