I would like typeahead.js to behave like jqueryui autocomplete with regards to how it matches items. Using jqueryui autocomplete it's possible to search inside the text items. In typeahead it's only from the beginning of the string.
Autocomplete example: http://goo.gl/O43afF
Typeahead example: http://twitter.github.io/typeahead.js/examples/
With autocomplete, it seems like it's possible to assign a comparison function, but I haven't found anything like that in typeahead.
If I have a list that contains the item "Equestrian (Horses)" then I would like to get a match if I start writing "o".
Typeahead.js code as is will look for prefix matches, as you correctly say. There is a "trick" though: every datum may also contain a tokens element, which as the Typeahead documentation says is "a collection of strings that aid typeahead.js in matching datums with a given query".
The prefix matching is done against tokens. If you don't supply a tokens value for one of your datums, its value is tokenized (space-separated) for you. However, you could supply tokens to get what you want. For example, in your case you would supply a value of tokens that is all the unique substrings of all the words in your query string.
I suggest "all unique substrings of length >= 2", btw.
typeahead's datasource is set via the 'source' parameter. So it's perfectly ok to place another method instead an array in there. Also note that it internally expects an array of strings so you have to format everything to string.
Take a look at this fiddle for an example
EDIT: this example now always generates values from Test 0 to Test 9, so you can of course only check by entering parts of "test"
Related
I am using cookieconsent.js to show a popup for users to accept for my website. I need to stop the cookie consent popup from showing if a page has a certain query string.
The documentation for cookieconsent provides a solution to "blacklistPage" where I can "Specify pages using string or RegExp" that I want to prevent the popup from showing on.
This is fine until I try to use regex for a query string.
Example of path, filename and query string to match:
/sub-folder/file-name.shtml?value=pair
"blacklistPage": [
"/.*\?value=pair"
]
According to the documentation it's expecting either regex or a string but you're trying to pass regex in a string which isn't valid.
using a string : ‘/index.html’ (matches ‘/index.html’ exactly)
using RegExp : /\/page_[\d]+.html/ (matched ‘/page_1.html’ and ‘/page_2.html’ etc)
Additionally you're quoting the blacklistPage, this doesn't need to be quoted.
By removing the quotes and provide a standard JS regex format you can make the following:
blacklistPage: [
/\/.*\?value=pair/
]
Alternatively your use case is simple so you could just use a string and avoid regex:
blacklistPage: [
'/sub-folder/file-name.shtml?value=pair'
]
I have come to the conclusion, along with a friend who knows js much better than I, that the cookieconsent.js script will not allow query strings.
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.
all,
I'm working on a language-learning app that compares Chinese characters to a "dictionary" array and displays whether or not the character is in that dictionary, along with its pronunciation in pinyin and its English meaning.
I'm having trouble getting the results to display in order.Here's the bin: http://jsbin.com/julitiq/178/edit?html,css,js*
What I want to do is:
I have a string called rawText, obtained from the ng-model of a textarea, and I want to go through it character by character.
Each character from rawText will be compared against the .chr attribute of each element in an array of objects ($scope.chrs).
If there is a match, I want to put the matching object from $scope.chrs into a second array of objects ($scope.infobox).
If there isn't a match, I want to make an empty object "template" (not a real template, just a dummy object), add the unmatched character to the empty object's .chr, and add this new object to $scope.infobox instead.
Finally, I want to display the contents of $scope.infobox in order using ng-repeat. Each div should contain the attributes of each individual object.
I'm no pro, so I'm sure there are better, more functional ways to do what I'm trying to do.
EDIT: I found out how to fix it using ng-change on the textarea to do the "objectifying", then running the object array using ng-repeat.
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.
I have something like the following;-
<--customMarker>Test1<--/customMarker>
<--customMarker key='myKEY'>Test2<--/customMarker>
<--customMarker>Test3 <--customInnerMarker>Test4<--/customInnerMarker> <--/customMarker>
I need to be able to replace text between the customMarker tags, I tried the following;-
str.replace(/<--customMarker>(.*?)<--\/customMarker>/g, 'item Replaced')
which works ok. I would like to also ignore custom inner tags and not match or replace them with text.
Also I need a separate expression to extract the value of the attribute key='myKEY' from the tag with Text2.
Many thanks
EDIT
actually I am trying to find things between comment tags but the comment tags were not displaying correctly so I had to remove the '!'. There's a unique situation that required comment tags... in anycase if anyone knows enough regex to help, it would be great. thank u.
In the end, I did something like the following (incase anyone else needs this. enjoy!!! But note: Word about town is that using regex with html tags is not ideal, so do your own research and make up your mind. For me, it had to be done this way, mostly bcos i wanted to, but also bcos it simplified the job in this instance);-
var retVal = str.replace(/<--customMarker>(.*?)<--\/customMarker>/g, function(token, match){
//question 1: I would like to also ignore custom inner tags and not match or replace them with text.
//answer:
var replacePattern = /<--customInnerMarker*?(.*?)<--\/customInnerMarker-->/g;
//remove inner tags from match
match = $.trim(match.replace(replacePattern, ''));
//replace and return what is left with a required value
return token.replace(match, objParams[match]);
//question 2: Also I need a separate expression to extract the value of the attribute key='myKEY' from the tag with Text2.
//answer
var attrPattern = /\w+\s*=\s*".*?"/g;
attrMatches = token.match(attrPattern);//returns a list of attributes as name/value pairs in an array
})
Can't you use <customMarker> instead? Then you can just use getElementsByTagName('customMarker') and get the inner text and child elements from it.
A regex merely matches an item. Once you have said match, it is up to you what you do with it. This is part of the problem most people have with using regular expressions, they try and combine the three different steps. The regex match is just the first step.
What you are asking for will not be possible with a single regex. You're going to need a mini state machine if you want to use regular expressions. That is, a logic wrapper around the matches such that it moves through each logical portion.
I would advise you look in the standard api for a prebuilt engine to parse html, rather than rolling your own. If you do need to do so, read the flex manual to get a basic understanding of how regular expressions work, and the state machines you build with them. The best example would be the section on matching multiline c comments.