I'm using mongodb in javascript and I was wondering how to effectively query names in a collection. As it is, I have 2 fields for my collections for the same thing which is pretty reduntent.
queryName: String
and name: String
The reason I do this is because if I have the name "John" for name, and I try try query the collection for the name "john"(note how the j is lowercase). It will return null. That's why I have the field queryName that takes the name and puts it into a format that's searchable. I was wondering if there's an option to disable case sensitivity and getting rid of spaces?
Use regex:
db.collection.find({name: /^john$/i });
Not sure what you mean when explaining why you have queryName and name, unless the former is some sort of normalization of the name field.
If you want to get rid of spaces, you'll have to be more specific. Spaces surrounding the query? Spaces in-between characters? You can do either with regex, though the latter is more cumbersome. A common practice is to trim() your data before you store it, though, so you don't have this problem.
db.collection.find({name: /^\s*john\s*$/i });
I'll leave it to OP to modify use for more complicated white space handling.
Related
I am making a filter for a chat room I own.
I was succesful in having it turn NSFW words into a bunch of symbols and astericks to censor it, but many people bypass it by simply putting a backslash, period, or other symbol/letter after it because I only put in the words without the punctation and symbols. They also come up with a bit more creative methods such as eeeNSFWeee so the filter doesn't count it as a word.
Is there a way to make it so that the filter will select certain characters that form a word in a string and replace them (with or without replacing the extra characters connected to the message)?
The Filter is made in javascript and Socket.io
Filter code:
const array = [
"NSFW",
"Bad Word"
"Innapropiate Word"
];
message = message
.split(" ")
.map((word) => (array.includes(word.toLowerCase()) ? "$#!%" : word))
.join(" ");
For an example if somebody typed "Bad Word" exactly like that (caps are not a problem), it would censor it succesfully.
But if somebody typed "Bad Word." that would be a problem because since it has a period it would count it as a different word, thats what I need fixed.
There are a number of approaches you could take here.
You could use replace() if you just want to remove symbols. For example:
word.replace(/[&\/\\#,+()$~%.`'"!;\^:*?<>{}_\[\]]/g, '')
You could use Regular Expressions in general, which allows you to match on patterns instead of exact string matching.
You could also use more complex fuzzy matching libraries or custom fuzzy matching to accomplish your goal. This post may be helpful.
I try to match multiple values between quotes
(these values can be anything but spaces)
the best I can achieve is to match everything between the first and the last quote
I already checked many SO answers, yet I cannot make it work
here is the regex
\[\[\[(\w*img\w*)\s(\w*id|url\w*)+="([^"]|.*)"\]\]\]
here is the string I try to match (values are numbers but I could have urls or anything similar)
[[[img id="37" w="100" h="70"]]]
I should get all parameters and their respecting values, but I get only one parameter with the value beeing 37" w="100" h="70
I know I am close, but this one is tricky
regards
I don't think you need all the \w.
And I also would suggest splitting the task in two parts as suggested in a comment.
However, I also see an option in doing it in just one step:
\[\[\[img(?:\s(\w+)="([^"]+)")?(?:\s(\w+)="([^"]+)")?(?:\s(\w+)="([^"]+)")?\]\]\]
This is basically the wrapper [[[]]], a normal character part img and then (?:\s(\w+)="([^"]+)")? repeated as many times as you expect attributes to appear. (\w+) matches the name of the attribute and ([^"]+) its value.
I want to get all the words, except one, from a string using JS regex match function. For example, for a string testhello123worldtestWTF, excluding the word test, the result would be helloworldWTF.
I realize that I have to do it using look-ahead functions, but I can't figiure out how exactly. I came up with the following regex (?!test)[a-zA-Z]+(?=.*test), however, it work only partially.
http://refiddle.com/refiddles/59511c2075622d324c090000
IMHO, I would try to replace the incriminated word with an empty string, no?
Lookarounds seem to be an overkill for it, you can just replace the test with nothing:
var str = 'testhello123worldtestWTF';
var res = str.replace(/test/g, '');
Plugging this into your refiddle produces the results you're looking for:
/(test)/g
It matches all occurrences of the word "test" without picking up unwanted words/letters. You can set this to whatever variable you need to hold these.
WORDS OF CAUTION
Seeing that you have no set delimiters in your inputted string, I must say that you cannot reliably exclude a specific word - to a certain extent.
For example, if you want to exclude test, this might create a problem if the input was protester or rotatestreet. You don't have clear demarcations of what a word is, thus leading you to exclude test when you might not have meant to.
On the other hand, if you just want to ignore the string test regardless, just replace test with an empty string and you are good to go.
I have a simple filter for searching images saved in a database. And therefore I use regex:Images.find({"name":{$regex:".*"+query+".*"}});
Of course I check the value with check(query, String); function. Could it be a big security issue, if I don't escape the special characters in the regex (query var, whose content is specified by user)? It is an advantage for me, that the users can define something like (nameOfImage1|nameOfImage2).
According to #Michel Floyd´s comment above, that is not a security problem, I use Regex in find(). But I also replaced some selected characters with query.replace(/[\/\\^$*[\]{}]/g, "");
As the title states, I can't get any way to filter all CommentThreads using the "?" searchTerm, this returns an empty list of CommentThreads.
Tried \? , \?, plain %03F, unicode \u0003F. Nothing seems to work.
Is there a way to filter by the questionmark ? I'm trying to retrieve all the questions from a video and if I can't filter this way it means I have to pull down everything and filter locally, which is really expensive (quota-wise).
Here is the api explorer url :
https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.commentThreads.list?part=id%252Csnippet&maxResults=10&order=time&searchTerms=%253F&textFormat=html&videoId=o4lMYiwKYRs&fields=items(replies%252Csnippet)%252CnextPageToken&_h=18&
I don't know if this is a bug or not, but it seems that the searchTerms parameter has its own rules to make itself works or filter the results.
Let us use this videoId=2ecT9zf1QZU that has a comments of:
24
kobe bryant?
24kobe
?kobe
kobe? bryant
IDOL24
If we use searchTerms=?, we will get 0 result like the one you experienced, because I think it does not work with special character alone or words that have this special character. (eg. ?)
If we use searchTerms=kobe, we will get 3 results, (kobe bryant?, ?kobe and kobe? bryant) so it work with a word without special character.
Now, we use searchTerms=?kobe and searchTerms=kobe?, we will get 0 result because of the special character ?.
What if we use searchTerms=yant (from the word bryant), the result is 0, because there is no word in the result that have a word yant.
So meaning there are some rules or limitation in using this searhTerms.
I hope this information helps you.