Dexie.js using variable in LIKE operator

im using this code

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.open();
db.friends.filter (function (friend) { return /Bar/.test(friend.name); })
.toArray()
.then(function(result) {
    alert ("Found " + result.length + " friends containing the word 'Bar' in its name...");
});

I wanted to know how I can use a variable instead of the search?

Ex.

var VarTest = 'Dani';
db.friends.filter (function (friend) { return /VarTest/.test(friend.name); })

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

  1. new RegExp(VarTest): This creates a regular expression from the value of VarTest. For example, if VarTest is ‘Dani’, it becomes equivalent to /Dani/.
  2. .test(friend.name): This checks if the name field of each friend object matches the pattern defined by the regex.
  3. 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’).