Jquery Autocomplete custom return data - javascript

Hello i'm using Jquery autocomplete for a project but i've a particular problem with my language. Here in albania our alphabet includes characters like ë and ç but some people don't know keyboard combination to produce this character and i'm trying to return these words using 'e' and 'c'. Example when people search for 'Dhermi' i want to show the word 'Dhërmi' as a suggestions. Same for the word "canta" i want to show "çanta". Here are many words like these so i want a function to return data in this way.
Below is my function
$("#search").autocomplete({
minLength:0,
delay:10,
appendTo: ".search",
source:function(request, response) {
//
var matchernormal = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
matcher = $.grep(tagsarr, function( item ){return matchernormal.test(item);})
console.log(matcher)
// Limit Results
var results = $.ui.autocomplete.filter(matcher, request.term);
response(results.slice(0, 10));
}
});
I'll be grateful if someone could help me with this problem that i've encountered.

What you are looking for is accent folding:
https://jqueryui.com/autocomplete/#folding
you can have a map of the accent characters to the normalised characters
var accentMap = {
"á": "a",
"ö": "o"
};
and a function to convert one from other (see the example link)
var normalize = function( term ) {....}
and compare with the normalised value of the string as well as the original one:
return matcher.test( value ) || matcher.test( normalize( value ) );

Related

jQuery Autocomplete: How to match entire/full words from array

I have an autocomplete that has multiple categories with values that matches any set of letters from the users input. Each category value has multiple strings stored in it (see value.application sample data below).
This works fine, but I want it to only match entire words intead of letter combination. How can this be achieved?
Example:
Source Data: "all caller tallest"
User Enters: "all"
Returns: "all caller tallest"
Looking for it to only return: "all"
source: function(request, response) {
var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); // match letters in string
response($.grep(SearchList, function(value) {
return matcher.test(value.label) ||
matcher.test(value.value) ||
matcher.test(value.sku) ||
matcher.test(value.application) ||
matcher.test(value.discontinuedproductlist) ||
matcher.test(value.type);
}));
},
As mentioned, each of these values, has multiple strings of words within them.
Example of (value.application) data: "dry wet cold warm hot burned charred dirty clean soiled"
Was able to make this work by adding the "\b" regex property to each side of the search term (see matcherWords).
Now i can return letters combination matches for some of the categories, and only whole word matches for the others. Hopefully this helps someone else.
source: function(request, response) {
var matcherLetters = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); // match letters in string
var matcherWords = new RegExp("\\b" + $.ui.autocomplete.escapeRegex(request.term) + "\\b", "i"); // match words in string
response($.grep(SearchList, function(value) {
return matcherLetters.test(value.label) ||
matcherLetters.test(value.value) ||
matcherWords.test(value.sku) ||
matcherWords.test(value.application) ||
matcherWords.test(value.discontinuedproductlist) ||
matcherWords.test(value.type);
}));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

How to clean string from word characters, Javascript

I'm trying to clean strings which has been transformed from word text but I'm stuck on removing special character '…'
By click on button "clean", script removes all dots and only one special character, however I need to remove all of them
Where is my mistake?
Here is my code and plunker with struggles
$scope.string = "My transformed string ………….........…...."
$scope.removeDots = function () {
var em = document.getElementsByTagName('em');
var reg = /\./g;
var hellip = /…/g
angular.forEach(em, function (item) {
if(item.innerText.match(reg)){
item.innerText = process(item.innerText)
}
if (item.innerText.match(hellip)){
item.innerText = item.innerText.replace("…", "")
}
});
};
function process( str ) {
return str.replace( /^([^.]*\.)(.*)$/, function ( a, b, c ) {
return b + c.replace( /\./g, '' );
});
}
There's a few problems here, but they can all be resolved by simply reducing the code to a single regex replace within process that will handle both periods and … entities:
$scope.removeDots = function () {
var em = document.getElementsByTagName('em');
angular.forEach(em, function (item) {
item.innerText = process(item.innerText)
});
};
function process( str ) {
return str.replace( /\.|…/g, '');
}
});
Plunker demo
You replace every occurrence of . in process, but only replace … once.
I don't see why don't you just do something like .replace(/(\.|…)/g, ''); the g modifier makes sure every match is replaced.
You can do both replacements by first replacing the occurrences of … with one point (because it might be the only thing you find), and then replacing any sequence of points by one:
function process( str ) {
return str.replace(/…/g, '.').replace(/\.\.+/g, '.');
}
var test="My transformed string ………….........…....";
console.log(process(test));
One of the reasons your code did not replace everything, is that you used a string as find argument, which will result in one replacement only. By using the regular expression as find argument you can get the effect of the g modifier.

How to create autocomplete which only starts with either first letter or any other letter in the string starting after space

In this fiddle autocomplete if I type letter "s" it shows listing which starts with "s" but I need all the listing starting with "s" and having space before "s".
Means if I type "s" then i should get both kind of results like "suraj kumar" & "nimish shah". Please someone suggest me solution.
jsfiddle.net/9R4cV/701/
I'm not a RegExp guru, so there might be more clever solutions, but here's an updated fiddle: http://jsfiddle.net/9R4cV/702/
source: function(req, responseFn) {
var re = $.ui.autocomplete.escapeRegex(req.term);
var matcher = new RegExp( "^" + re, "i" );
var matcher2 = new RegExp( "\\s" + re, "i" );
var a = $.grep( aTags, function(item,index){
return matcher.test(item) || matcher2.test(item);
});
responseFn( a );
}

How to compare strings in javascript ignoring special characters

I'm developing an app and I've been asked to compare strings, but text in string have special characters (spanish accents like "á", "é", "í", "ó" and "ú")
I already manage capitalization with toUpperCase(), but still, I want to be sure that I have no problem with accents.
What I have to do is to compare some words already saved in system and check if used typed any of them.
What I do is store the typed words in an array, and then proceed to analyze them in another function (yet to be implemented)
This is my function where I store the words the user types (it may change to make it more complete):
function clickNewWord(){
var theWord = textField.value.toUpperCase();
ArrayWrittenWords.push(theWord);
textField.value = "";
}
PD: I'll take the opportunity to ask: What would be the correct coding to work with accents? UTF-8?
Although its an old question however, for the sake of future googlers here is the best way to remove accent from a string:
var string = 'á é í ó ú';
string.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
>a e i o u
You can convert them and then match them, let me if my example is clear :)
var stringInTheSystem = ['aaaa','bbbb'];// Array of string in your system;
var term = 'áaaa';// the word you want to compare it;
term = term.replace(/á/g, "a");
term = term.replace(/é/g, "e");
term = term.replace(/í/g, "i");
term = term.replace(/ó/g, "o");
term = term.replace(/ú/g, "u");
var matcher = new RegExp( term, "i" );
$.grep( stringInTheSystem, function( value ) {
value = value.test || value.value || value;
console.log(matcher.test( value ));
});

javascript fail - regexp bug

Hei all
I have this code
function prototype( str , id )
{
var ret = str;
ret = ret.replace( /ø/g, 'oe' );
ret = ret.replace( /Ø/g, 'OE' );
ret = ret.replace( /å/g, 'aa' );
ret = ret.replace( /Å/g, 'AA' );
ret = ret.replace( /æ/g, 'ae' );
ret = ret.replace( /Æ/g, 'AE' );
document.getElementById( id ).value = ret.replace(/[^a-zA-Z0-9\/\_-]/i,'_').replace(/_+/g,'_');
}
My problem is now, if i use word like this (demo demo demo) its okay make this word to (demo_demo demo)
i use this function to escape urls. the next i need its send it to lower case, after I'm done, i hope for help :)
tanks a lot all.
You forgot the greedy-modifier
/[^a-zA-Z0-9\/\_-]/i
....
/[^a-zA-Z0-9\/\_-]/ig
If the problem is that only the first space gets replaced with _, then you need to put the g option to the regex replace.
ret.replace(/[^a-zA-Z0-9\/\_-]/gi,'_')
to turn the string to lower case use the
toLowerCase() method of strings.
Assuming that your 1st requirement is to replace space, +, - characters with underscore,
document.getElementById( id ).value = ret.replace(/[^a-zA-Z0-9\/\_-]/ig,'_').replace(/_+/ig,'_');
Other requirement is to make it lowercase string
document.getElementById( id ).value = document.getElementById( id ).value.toLowerCase();
The main potential issue I see, is on first replacement (on Ø and other scandinavian characters).
you should change with unicode char representation, e.g. this statement
ret = ret.replace( /Ø/g, 'OE' );
should be
ret = ret.replace( /\u0153/g, 'OE' );
for other diacritic signs just find a unicode chart like http://www.chucke.com/entities.html

Categories

Resources