[JavaScript] Strip accents from a string
Published on 2019-01-06 • Modified on 2019-01-06
This one is very useful because it allows you to do accent insensitive searches on the user input. It allows to have the same results if the user types the accents or not. Open the browser console to check the results.
/**
* I am using a JavaScript module to isolate the code of each snippet.
* In fact it's a Vue.js mixin. Take the code called by the mounted()
* or the snippetXX() function.
*/
export default {
methods: {
snippet10 () {
const strWithAccents = 'Lot of accents there çéâêîïôûàèùœÇÉÂÊÎÏÔÛÀÈÙŒ wow!'
console.log('strWithAccents: ' + strWithAccents)
console.log('strWithoutAccents: ' + this.stripAccents(strWithAccents))
},
stripAccents (str) {
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
}
},
mounted () {
if (this.isArticle(10)) {
this.snippet10()
}
}
}
More on Stackoverflow Random snippet