admin管理员组

文章数量:1402958

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
Add a comment  | 

1 Answer 1

Reset to default 3

You 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');

本文标签: javascriptdexiejs using variable in LIKE operatorStack Overflow