Chosen jQuery plugin search with spaces - javascript

I have a problem with the Chosen jQuery plugin. When I try to search for a string which has space in it I get no results even if it does exist.
For example:
If I enter the string "and barbu" I don't get anything back. But when I write "antigua and barbu" I get the result.
What should I do to fix this space problem?

Answer here : Changing search behavior in jquery plugin Chosen
Just need to add
jQuery('select').chosen({search_contains:true}) ;
As mentionned on the options doc :
http://harvesthq.github.io/chosen/options.html
search_contains false By default, Chosen’s search matches starting at the beginning of a word. Setting this option to true allows matches starting from anywhere within a word. This is especially useful for options that include a lot of special characters or phrases in ()s and []s.

Related

JS: Check if word "handover" contains "hand"

I'm working on this simple, straightforward text content filtering mechanism on our post commenting module where people are prohibited from writing foul, expletive words.
So far I'm able to compare (word-by-word, using .include()) comment contents against the blacklisted words we have in the database. But to save space, time and effort in entering database entries for each word such as 'Fucking' and 'Fuck', I want to create a mechanism where we check if a word contains a blacklisted word.
This way, we just enter 'Fuck' in the database. And when visitor's comment contains 'Fucking' or 'Motherfucker', the function will automatically detect that there is a word in the comment that contain's 'fuck' in it and then perform necessary actions.
I've been thinking of integrating .substring() but I guess that's not what I need.
Btw, I'm using React (in case you know of any built-in functions). Much as possible, I wanna deviate from using libraries for this mechanism.
Thanks a heap!
"handover".indexOf("hand")
It will return index if it exists otherwise -1
To ignore cases you can define all your blacklisted words in lower case and then use this
"HANDOVER".toLowerCase().indexOf("hand")
To detect if a string has another string inside of it you can simply use the .includes method, it does not work on a word by word basis but checks for a sequence of characters so it should meet you requirements. It returns a boolean value for if the string is inside the other string
var sentence = 'Stackoverflow';
console.log(sentence.includes("flow"));
You were on the right track with .includes()
console.log('handover'.includes('hand'));
Returns true

Can a regex expression on javascript be started with an exclamation mark?

Okay so let me be clear about this, I have a user interface where a user can search for products based on their product title, and the way it's built is that you don't need to type the full exact title to get a match. Basically under the hood it uses '$regex' operator on the find method, so if I type /banana/ it retrieves any product that contains on the title the word banana.
The good part of this is that if I type:
/^((?!banana).)*$/
It negates it, and returns any product that doesn't contain the word banana.
What I am trying to achieve is giving the negation feature to the user on a more friendly way instead of using the whole regex above.
So I thought about telling the user to use exclamation mark on the start of text and then under the hood replace it by this regex wrapper /^((?!banana).)*$/ . The problem is that I will lose functionality if there is any valid regex that starts by exclamation mark, because I will always be replacing the search tag with the negation wrapper. Does it make sense?
Thank you

Match a words, which contains specific symbols

It seems that i'm stuck with something simple, but I was unable to find quite similar question on stack.
Using JavaScript/jQuery/regexp I want to match a words that contains specific symbols in string .
I.e in given string 'check out mydomain/folder/#something' if i run this kind of search with symbols 'folder/#' it must return whole mydomain/folder/#something.
In fact I want to use this to replace whole link in a string with some kind of widget button, but as those links are pretty specific (i.e i know that they will contain folder/#) using some kind of library for this task would be overkill.
Here is the regexp you are looking for: /[\w\/]*\/folder\/#[\w\/]*/
var str = 'check out mydomain/folder/#something';
// returns ["mydomain/folder/#something"]
str.match(/[\w\/]*\/folder\/#[\w\/]*/)
Or for a more robust version or it: /[\w\/\.]*\/folder\/#(?:[\w\/]|\.\w+)*/
That last one will accept dots in the file names but ignore the last one.
For instance 'check out my.domain/foo/folder/#some.thing.'will return ["my.domain/foo/folder/#some.thing"]

jquery select2 plugin regex for allowed characters?

Is there anyway to define a set of "allowed" characters for user input? For e.g. alphanumeric input only. Can't seem to find it within the official documentation
You can accomplish this using the createSearchChoice option, which allows you to define a custom function to create tags from user input. To not allow tags containing other than alphanumeric characters, the select2 should be initialized as:
$("#mySelect").select2({
createSearchChoice: function(term) {
if(term.match(/^[a-zA-Z0-9]+$/g))
return { id: term, text: term };
},
formatNoMatches: "Enter valid format text"})
If the user is typing text that contains non-alphanumeric characters, the dropdown below the select2 will show no matches. It's not necessary to specify a custom formatNoMatches but it is more helpful for the user than the default "No matches found" to figure out why their input isn't being accepted.
Update:
As of select2 version ~ 4.0 this function is now called createTag
I didn't find anything about put a mask/regex on the input where you type.
But you can do it on the 'open' event provided by select2.
Something like this:
$('select of the select2 input or select').on('select2-open',function(e){
// apply your mask on this element -> $('.select2-input')
});
Hope it helps you.
Bye
Today I was finding a way to apply a custom mask on the input that appear in the dropdown (select2 v3), and I found this topic.
After some research on official doc I saw the method select2("container"), and looking the source code I found select2("dropdown") (not present on doc).
So, my final code is (for contribution purpose):
$('my-select2').on('select2-open',function(e){
var input = $(this).select2("dropdown").find('.select2-input');
// ...
});

Hide characters in a string

I was wondering if there was a way to hide a string of characters in a string. I found Control Characters which work for hidding those characters:
>var hidden = "\26"
undefined
>hidden
""
>hidden.replace("\26","yolo");
"yolo"
>"".replace("\26","yolo");
""
but what i would like to escape a string of characters and have them not show up like this:
>var hidden = "\26cantseethis\26"
undefined
>hidden
""
Is there any such method using ASCII characters?
edit:
What I am trying to do is give state to a google doc. I have a workflow type google app script attached to a form that creates a doc. the doc is immediatly viewable by the administrator so i dont want to put a bunch of special strings like &UserOneAgreed in the doc, mostly because of the potential of someone going in and modifying that string. I have another script that will go in and modify the related text once some user input is gathered.
You cannot do that. The control character can be used for character only, so you will need to escape each character separately to hide them.

Categories

Resources