I want to have valid query like that countries=fr,be
I tried this:
countries: Joi.string().valid(['fr','be'])
But this allows only to have countries=fr or countries=be, but not multiple comma separated values. How can I achieve this?
You can use regex like so:
var list = ['fr', 'be']; // your counrty codes
var joinedList = '(' + list.join('|') + ')'; // prepare RegExp cases
var regex = new RegExp('^' + joinedList + '(,' + joinedList + ')*$') // the thing in itself
Then:
countries: Joi.string().regex(regex).required()
It would match any country code in list, alone or in a comma-separated list. I added required() as I believe valid() implicitly makes the field required as long as you don't allow undefined value.
Related
we are using azure search API but are getting no results for special characters that contain no alphanumeric characters.
We was having trouble returned any matching results for Japanese language and any special characters at all until we wrapped the string in quotes (") see the examples below (we are escaping the special characters also.
strings that did not work
var searchTerm = "嘘つきな唇で";
var searchTerm = "test#123";
var searchTerm = "?sd-^&*d$£(";
After wrapping in quotes i.e.
searchTerm = "\"" + searchTerm + "\"*"
all the above searches returned the expected matches but now we have an issue of no matches with strings with only special characters in i.e.
var searchTerm = "####";
var searchTerm = "&#*(%$";
new SearchParameters
{
SearchFields = new List<string> {"name", "publicId"},
Top = 50,
SearchMode = SearchMode.Any,
QueryType = QueryType.Simple,
Filter = $"status eq 1"
}
Any help on this would be greatly appreciated
Kind regards
Without escaping or using another analyzer (rather than StandardAnalyzer which is the default) I don't think you'll be able to retrieve the results as some of the samples you've provided are reserved / special chars:
Please check:
https://learn.microsoft.com/en-us/azure/search/query-lucene-syntax#escaping-special-characters
EDIT: please read about analyzers in here: https://learn.microsoft.com/en-us/azure/search/search-analyzers
I want to search values having special characters such as "(" in a document.
I 'm using following criteria in mongoose and fetch the names that matches like "abc (pvt) ltd".
var criteria = {};
criteria.name = new RegExp(searchPrameters.name, "i");
I found the solution with replacing of the parenthesis using string.replace() function.
searchPrameters.name = searchPrameters.name.replace('(','\\(');
searchPrameters.name = searchPrameters.name.replace(')','\\)');
I have an object from which I extract values like so if I want a list:
each gear_tag in store.GearTags
li #{gear_tag.Tag.tag_name}
Now I want to concatenate all tag_names with ', ' in between. I mean that I want the resulting string to be something like:
"tag_name_1, tag_name_2, tag_name_3, tag_name_4" if the object has 4 gear_tags.
How do I achieve this?
You can use the array .map method combined with the .join method.
var tagString = store.GearTags.map(function (gear_tag) {
return gear_tag.Tag.tag_name;
}).join(', ');
Given:
store.GearTags = [
{Tag: {tag_name: 'tag_name_1'}},
{Tag: {tag_name: 'tag_name_2'}},
{Tag: {tag_name: 'tag_name_3'}}
];
The above logic would yield:
"tag_name_1, tag_name_2, tag_name_3"
Array.map iterates over every item in an array and lets you return a new value for that position in a new array that .map returns. Then Array.join does exactly what you want, returning a concatenated string of all items in the array.
With .map we create a new array with only the tag name strings in it. Then with .join we join them all together into one big comma-separated string.
Sad that I did not figure this out earlier.
- var desc_tags = ''
each gear_tag, i in store.GearTags
- if(i == retailer.GearTags.length - 1)
- desc_tags = desc_tags + gear_tag.Tag.tag_name
- else
- desc_tags = desc_tags + gear_tag.Tag.tag_name + ", "
I was typing a "- " before the "each" statement, which was causing the code not to work.
What if you do:
- var desc_tags = tags.map(function(e) {return e.Tag.tag_name}).join(', ');
I'm trying to do a search on items that contain a certain substring, but I'm getting no results back (even though I know the data is right for the query, including case):
collection.find({name: "/.*" + keyword + ".*/"}).toArray(function(err, items)
Should that not match everything that contains the keyword? It just returns an empty object.
I'm just using the regular MongoDB driver in an ExpressJS app.
You need to build a regular expression first should try something like this:
var regex = RegExp("/.*" + keyword + ".*/")
Then pass in the variable to the query. I generally find it easier to do the query as a variable and pass that in:
var query = { FieldToSearch: new RegExp('^' + keyword) };
collection.find(query).toArray(...)
I've included the regex as a left rooted regex to take advantage of indexes (always recommended if possible for performance). For more take a look here:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-RegularExpressions
I did it like this
keyword = "text_to_search";
collection.find({name: {$Regex: keyword, $options:$i }})
I used i $i make the query case insensitive but u can use other options too
try this:
var keyword = req.params.keywords;
var regex = RegExp(".*" + keyword + ".*");
Note.find({noteBody: regex, userID: userID})
I got the keywords from the request parameters and I want to search from the noteBody with these keywords, now the keywords is a variable. If you want to put a variable in the database find, the format must be var regex = RegExp("." + keyword + "."). Hope this helps. Thanks
I am currently working on a project that dynamically displays DB content into table.
To edit the table contents i am want to use the dynamically created "string"+id value.
Is there any way to retrieve the appended int value from the whole string in javaScript?
Any suggestions would be appreciative...
Thanks!!!
If you know that the string part is only going to consist of letters or non-numeric characters, you could use a regular expression:
var str = "something123"
var id = str.replace(/^[^\d]+/i, "");
If it can consist of numbers as well, then things get complicated unless you can ensure that string always ends with a non-numeric character. In which case, you can do something like this:
var str = "something123"
var id = str.match(/\d+$/) ? str.match(/\d+$/)[0] : "";
(''+string.match(/\d+/) || '')
Explanation: match all digits in the variable string, and make a string from it (''+).
If there is no match, it would return null, but thanks to || '', it will always be a string.
You might try using the regex:
/\d+$/
to retrieve the appended number