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); })
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); })
Share
Improve this question
asked Mar 26 at 22:38
Dani CarlaDani Carla
938 bronze badges
1
- do mean can you bind the regex to a variable? yes?.. VarTest.test(friend.name) – danielRICADO Commented Mar 26 at 22:49
1 Answer
Reset to default 3You need to use //
when assigning to VarTest
:
var VarTest = /Dani/;
db.friends.filter (function (friend) { return VarTest.test(friend.name); })
If you want to make the regular expression dynamically from a string, use the RegExp
constructor:
var VarTest = new RegExp('Dani');