How to return string by matching two values with similar characters?












0















Not much helpful question title. Let me explain: For a search functionality, I use the code below to return items that match a search query:



filter(value: string, query: string): boolean {
return value.toLowerCase().indexOf(query.toLowerCase()) > -1;
}

filteredItems() {
return this.items.filter(i => this.filter(i.value, this.queryString));
}


I have a case where I need to return an item whose value is DIGER if query string's value, containing Turkish characters, is DİĞER and vice versa.



I tried



if(query.includes('ğ') || query.includes('ş') || query.includes('ö') || query.includes('ü') || query.includes('ı') || query.includes('ç')) {
return value.toLowerCase().indexOf(query.replace('ğ', 'g').replace('ş', 's').replace('ö', 'o').replace('ü', 'u').replace('ı', 'i').replace('ç', 'c').toLowerCase()) > -1;
}


Not working quite as I demand, it only converts the Turkish characters, but I couldn't find any solution. How would I accomplish such a task?










share|improve this question























  • So, what else do you want to convert?

    – Andreas
    Nov 22 '18 at 7:03
















0















Not much helpful question title. Let me explain: For a search functionality, I use the code below to return items that match a search query:



filter(value: string, query: string): boolean {
return value.toLowerCase().indexOf(query.toLowerCase()) > -1;
}

filteredItems() {
return this.items.filter(i => this.filter(i.value, this.queryString));
}


I have a case where I need to return an item whose value is DIGER if query string's value, containing Turkish characters, is DİĞER and vice versa.



I tried



if(query.includes('ğ') || query.includes('ş') || query.includes('ö') || query.includes('ü') || query.includes('ı') || query.includes('ç')) {
return value.toLowerCase().indexOf(query.replace('ğ', 'g').replace('ş', 's').replace('ö', 'o').replace('ü', 'u').replace('ı', 'i').replace('ç', 'c').toLowerCase()) > -1;
}


Not working quite as I demand, it only converts the Turkish characters, but I couldn't find any solution. How would I accomplish such a task?










share|improve this question























  • So, what else do you want to convert?

    – Andreas
    Nov 22 '18 at 7:03














0












0








0








Not much helpful question title. Let me explain: For a search functionality, I use the code below to return items that match a search query:



filter(value: string, query: string): boolean {
return value.toLowerCase().indexOf(query.toLowerCase()) > -1;
}

filteredItems() {
return this.items.filter(i => this.filter(i.value, this.queryString));
}


I have a case where I need to return an item whose value is DIGER if query string's value, containing Turkish characters, is DİĞER and vice versa.



I tried



if(query.includes('ğ') || query.includes('ş') || query.includes('ö') || query.includes('ü') || query.includes('ı') || query.includes('ç')) {
return value.toLowerCase().indexOf(query.replace('ğ', 'g').replace('ş', 's').replace('ö', 'o').replace('ü', 'u').replace('ı', 'i').replace('ç', 'c').toLowerCase()) > -1;
}


Not working quite as I demand, it only converts the Turkish characters, but I couldn't find any solution. How would I accomplish such a task?










share|improve this question














Not much helpful question title. Let me explain: For a search functionality, I use the code below to return items that match a search query:



filter(value: string, query: string): boolean {
return value.toLowerCase().indexOf(query.toLowerCase()) > -1;
}

filteredItems() {
return this.items.filter(i => this.filter(i.value, this.queryString));
}


I have a case where I need to return an item whose value is DIGER if query string's value, containing Turkish characters, is DİĞER and vice versa.



I tried



if(query.includes('ğ') || query.includes('ş') || query.includes('ö') || query.includes('ü') || query.includes('ı') || query.includes('ç')) {
return value.toLowerCase().indexOf(query.replace('ğ', 'g').replace('ş', 's').replace('ö', 'o').replace('ü', 'u').replace('ı', 'i').replace('ç', 'c').toLowerCase()) > -1;
}


Not working quite as I demand, it only converts the Turkish characters, but I couldn't find any solution. How would I accomplish such a task?







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 7:01









Ahmet ÖmerAhmet Ömer

8817




8817













  • So, what else do you want to convert?

    – Andreas
    Nov 22 '18 at 7:03



















  • So, what else do you want to convert?

    – Andreas
    Nov 22 '18 at 7:03

















So, what else do you want to convert?

– Andreas
Nov 22 '18 at 7:03





So, what else do you want to convert?

– Andreas
Nov 22 '18 at 7:03












1 Answer
1






active

oldest

votes


















1














The problem looks to be that value.toLowerCase() may still include Turkish, so if the new query is searching for English characters only, it may not be found inside the new value.



One option would be to use an unconditional conversion of both the value and the query to English characters, after which you can use includes to check if the new value (the haystack) .includes the query (needle):



const chars = {
ğ: 'g',
ş: 's',
ö: 'o',
ü: 'u',
ı: 'i',
ç: 'c',
};
const convert = str => str
.replace(/[ğşöüıç]/g, char => chars[char])
.toLowerCase();


filter(value: string, query: string): boolean {
const [replacedValue, replacedQuery] = [value, query].map(convert);
return replacedValue.includes(replacedQuery);
}





share|improve this answer
























  • To make it work correctly with all cases(for example it didn't find 'YAZIŞMA' when 'yazisma' was searched. I put the toLowerCase() method before replacing the characters. But it solved my problem, accepting this as an answer. Thanks!

    – Ahmet Ömer
    Nov 22 '18 at 7:29











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425456%2fhow-to-return-string-by-matching-two-values-with-similar-characters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














The problem looks to be that value.toLowerCase() may still include Turkish, so if the new query is searching for English characters only, it may not be found inside the new value.



One option would be to use an unconditional conversion of both the value and the query to English characters, after which you can use includes to check if the new value (the haystack) .includes the query (needle):



const chars = {
ğ: 'g',
ş: 's',
ö: 'o',
ü: 'u',
ı: 'i',
ç: 'c',
};
const convert = str => str
.replace(/[ğşöüıç]/g, char => chars[char])
.toLowerCase();


filter(value: string, query: string): boolean {
const [replacedValue, replacedQuery] = [value, query].map(convert);
return replacedValue.includes(replacedQuery);
}





share|improve this answer
























  • To make it work correctly with all cases(for example it didn't find 'YAZIŞMA' when 'yazisma' was searched. I put the toLowerCase() method before replacing the characters. But it solved my problem, accepting this as an answer. Thanks!

    – Ahmet Ömer
    Nov 22 '18 at 7:29
















1














The problem looks to be that value.toLowerCase() may still include Turkish, so if the new query is searching for English characters only, it may not be found inside the new value.



One option would be to use an unconditional conversion of both the value and the query to English characters, after which you can use includes to check if the new value (the haystack) .includes the query (needle):



const chars = {
ğ: 'g',
ş: 's',
ö: 'o',
ü: 'u',
ı: 'i',
ç: 'c',
};
const convert = str => str
.replace(/[ğşöüıç]/g, char => chars[char])
.toLowerCase();


filter(value: string, query: string): boolean {
const [replacedValue, replacedQuery] = [value, query].map(convert);
return replacedValue.includes(replacedQuery);
}





share|improve this answer
























  • To make it work correctly with all cases(for example it didn't find 'YAZIŞMA' when 'yazisma' was searched. I put the toLowerCase() method before replacing the characters. But it solved my problem, accepting this as an answer. Thanks!

    – Ahmet Ömer
    Nov 22 '18 at 7:29














1












1








1







The problem looks to be that value.toLowerCase() may still include Turkish, so if the new query is searching for English characters only, it may not be found inside the new value.



One option would be to use an unconditional conversion of both the value and the query to English characters, after which you can use includes to check if the new value (the haystack) .includes the query (needle):



const chars = {
ğ: 'g',
ş: 's',
ö: 'o',
ü: 'u',
ı: 'i',
ç: 'c',
};
const convert = str => str
.replace(/[ğşöüıç]/g, char => chars[char])
.toLowerCase();


filter(value: string, query: string): boolean {
const [replacedValue, replacedQuery] = [value, query].map(convert);
return replacedValue.includes(replacedQuery);
}





share|improve this answer













The problem looks to be that value.toLowerCase() may still include Turkish, so if the new query is searching for English characters only, it may not be found inside the new value.



One option would be to use an unconditional conversion of both the value and the query to English characters, after which you can use includes to check if the new value (the haystack) .includes the query (needle):



const chars = {
ğ: 'g',
ş: 's',
ö: 'o',
ü: 'u',
ı: 'i',
ç: 'c',
};
const convert = str => str
.replace(/[ğşöüıç]/g, char => chars[char])
.toLowerCase();


filter(value: string, query: string): boolean {
const [replacedValue, replacedQuery] = [value, query].map(convert);
return replacedValue.includes(replacedQuery);
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 7:08









CertainPerformanceCertainPerformance

79.5k143865




79.5k143865













  • To make it work correctly with all cases(for example it didn't find 'YAZIŞMA' when 'yazisma' was searched. I put the toLowerCase() method before replacing the characters. But it solved my problem, accepting this as an answer. Thanks!

    – Ahmet Ömer
    Nov 22 '18 at 7:29



















  • To make it work correctly with all cases(for example it didn't find 'YAZIŞMA' when 'yazisma' was searched. I put the toLowerCase() method before replacing the characters. But it solved my problem, accepting this as an answer. Thanks!

    – Ahmet Ömer
    Nov 22 '18 at 7:29

















To make it work correctly with all cases(for example it didn't find 'YAZIŞMA' when 'yazisma' was searched. I put the toLowerCase() method before replacing the characters. But it solved my problem, accepting this as an answer. Thanks!

– Ahmet Ömer
Nov 22 '18 at 7:29





To make it work correctly with all cases(for example it didn't find 'YAZIŞMA' when 'yazisma' was searched. I put the toLowerCase() method before replacing the characters. But it solved my problem, accepting this as an answer. Thanks!

– Ahmet Ömer
Nov 22 '18 at 7:29


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425456%2fhow-to-return-string-by-matching-two-values-with-similar-characters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'