javascript
var db = new Dexie("likeTest");
db.version(1).stores({
friends: "name,age"
});
db.on('populate', function() {
db.friends.add({name: "Foo Barsson", age: 33});
db.friends.add({name: "Dani Smith", age: 25}); // Adding another example
});
db.open();
var VarTest = 'Dani'; // Your variable
db.friends.filter(function (friend) {
return new RegExp(VarTest).test(friend.name); // Dynamically create regex with variable
})
.toArray()
.then(function(result) {
alert("Found " + result.length + " friends containing '" + VarTest + "' in its name...");
});
Explanation
new RegExp(VarTest): This creates a regular expression from the value of VarTest. For example, if VarTest is ‘Dani’, it becomes equivalent to /Dani/.
.test(friend.name): This checks if the name field of each friend object matches the pattern defined by the regex.
Case Sensitivity: By default, RegExp is case-sensitive. If you want case-insensitive matching (e.g., to match “dani” or “DANI”), you can add the i flag like this: new RegExp(VarTest, ‘i’).