How can I perform a string search that doesn't take into account accented characters

Looking for a way to treat accented characters as equivalent to their non-accented counterparts in my code. Here's what I have so far:

var re = new RegExp(string, 'i');
if(target.search(re) == 0) { }

Currently, the code ignores the character's case. How can I also make it ignore whether the character is accented or not?

Answer №1

It seems like the best approach would be to eliminate the diacritics before applying your regular expression.
You might want to consider using this specific function that I stumbled upon here :

function removeAccentMarks(str)
{
 var patterns=[
 {pattern:/[\xC0-\xC6]/g, replacement:'A'},
 {pattern:/[\xE0-\xE6]/g, replacement:'a'},
 {pattern:/[\xC8-\xCB]/g, replacement:'E'},
 {pattern:/[\xE8-\xEB]/g, replacement:'e'},
 {pattern:/[\xCC-\xCF]/g, replacement:'I'},
 {pattern:/[\xEC-\xEF]/g, replacement:'i'},
 {pattern:/[\xD2-\xD6]/g, replacement:'O'},
 {pattern:/[\xF2-\xF6]/g, replacement:'o'},
 {pattern:/[\xD9-\xDC]/g, replacement:'U'},
 {pattern:/[\xF9-\xFC]/g, replacement:'u'},
 {pattern:/[\xD1]/g, replacement:'N'},
 {pattern:/[\xF1]/g, replacement:'n'} ];

 for(var j=0, length=patterns.length; j<length; j++)
  str=str.replace(patterns[j].pattern, patterns[j].replacement);

 return str;
}

Answer №2

utilizes the semplice library

var latin_map = {
  'Á': 'A', // LATIN CAPITAL LETTER A WITH ACUTE
  'Ă': 'A', // LATIN CAPITAL LETTER A WITH BREVE
...
  'ᵥ': 'v', // LATIN SUBSCRIPT SMALL LETTER V
  'ₓ': 'x', // LATIN SUBSCRIPT SMALL LETTER X
};


String.prototype.latinise = function() {
   return this.replace(/[^A-Za-z0-9]/g, function(x) { return latin_map[x] || x; })
};

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Retrieving information from a mongoDB query in a nodejs environment

I recently started learning the MEAN stack and encountered an issue. I need assistance in sending the following query data to the frontend. router.get('/average', (req, res) => { Employees.aggregate([ { $match: { "position": "sen" } }, ...

Oops! We encountered an internal issue: MongooseError: The operation `posts.insertOne()` has exceeded the buffering time limit of 100

Encountering an error when trying to post data to the Posts model upon clicking the post button: Internal error: MongooseError: Operation posts.insertOne() buffering timed out after 10000ms My setup includes a local MongoDB and Next.js 14 with app router. ...

Exploring the compatibility of Next.js with jest for utilizing third-party ESM npm packages

Caught between the proverbial rock and a hard place. My app was built using: t3-stack: v6.2.1 - T3 stack Next.js: v12.3.1 jest: v29.3.1 Followed Next.js documentation for setting up jest with Rust Compiler at https://nextjs.org/docs/testing#setting-up-j ...

Displaying overlay when sidebar menu is expanded

I am working on creating a smooth overlay transition for when the sidebar menu opens and closes. When the overlay is clicked, I want the menu to close. This is what I have so far: $(document).ready(function() { $('#menu-toggle').click(fun ...

Using the spread syntax to eliminate a property from an object within a ReactJs element

I'm trying to figure out if it's possible to remove a specific object property using spread syntax when rendering a React component. I want to achieve this without adding too much extra code. Currently, I am utilizing {reset,...inputName} In my ...

JavaScript tutorial: Locate a specific word in a file and display the subsequent word

Seeking assistance with a task: I need to open a locally stored server file, search for a specific word, and then print the next word after finding the specified one. I have managed to write code that successfully opens the file, but it currently prints e ...

Deleting an item from a JSON object using Angular

Whenever I click on an item in the list, it generates an input text and adds the attributes of that input to a JSON. Currently, each time I click on an item multiple times, the JSON accumulates all the clicks. For instance, if I click 3 times on the first ...

Resolving Cross-Domain Ajax for Improved API Performance

In the midst of developing an infrastructure to support a gaming platform that will cater to a large user base, performance is our top priority. We are striving to parallelize the architecture by running APIs, databases, and applications on separate server ...

Sending an Angular $http post request to a MVC action but the parameter is coming through as

After posting this content: $http.post(Common.blog.save, { blog: blog }) .then(saveBlogComplete) .catch(function(message) { }); The Fiddler output I receive is as follows: {"blog":{"title":"Chicken Is Good","content":"#Chicken Is Good\ ...

Retrieve all elements from an array using jQuery

How do I extract all the elements from the array outside of the function? $.each(Basepath.Templates, function(i){ templateArray = new Array({title: Basepath.Templates[i].Template.name, src: 'view/'+Basepath.Templates[i].Template.id, descri ...

The issue of the cursor not showing up in Chrome within a nested contenteditable structure arises when applying CSS after the

Currently, I am coding a HTML document that includes an element with the attribute contenteditable set to true, but this element is wrapped inside a parent element with contenteditable set to false. The HTML structure looks like this: <div contentedita ...

Tips on utilizing knockout validation

I stumbled upon this source for creating validations. However, I am struggling to grasp the concept of implementing the extend method in my code. I populate my observable with data received from a Breeze query. The loading process looks like this: data ...

The functionality of the ui sortable feature in Angular is not effective when used on a single-page website

My latest project involves using a single-page application. To connect the UI Angular library, I followed these steps: I started by adding the necessary scripts: <script src=.....jquery-1.9.1.js"></script> <script src=.....jquery-ui.js"> ...

Activate the Keypress event to update the input value in React upon pressing the Enter

I am facing an issue where I need to reset the value of an input using a method triggered by onPressEnter. Here is the input code: <Input type="text" placeholder="new account" onPressEnter={(event) => this.onCreateAccount(event)}> < ...

Variable scope not properly maintained when there is a change in the Firebase promise

I am currently working on developing a controller function to handle signup submissions using Firebase. However, I've encountered an issue where the variables within the scope (controllerAs: $reg) do not seem to update correctly when modified inside a ...

Tips on handling a Vue computed property

I am struggling to dispatch an object that is created within a computed property in vue.js. Being fairly new to this framework, I can't seem to make it work. My goal is to dispatch the object named "updateObject" to the vuex-store. I have tried using ...

Wrapping an anchor tag with a div in Codeigniter

Can a div tag be used inside an anchor function? I have a div with the following CSS: #first{ opacity:0; } Now, I want to include it in my anchor element. Here is the code snippet: <?php if(is_array($databuku)){ echo '<ol>&l ...

Activate the enter key functionality with the help of knockout and a JavaScript function

My HTML textbox has an onkeypress event that triggers the sending of a message as shown below: <input type="text" data-bind="attr:{id: 'txtDim' + $data.userID, onkeypress: $root.sendMsg('#txtDim' + $data.userID, $data)}" /> I ...

What is the best way to trigger the second function on a click event?

Is there a way to trigger my 20 second timer with the click of a button, followed by an alert after the timer ends? HTML <aside class="start-box"> <button type="submit" class="btn btn-primary btn-lg" id="toggleBtn" onclick="startCloc ...

Refreshing a webpage following an AJAX call

Whenever I make a post request to an API, I receive a response. However, even though the data is saved when I hit the API, I have to manually refresh my blade.php page to see the newly added data. Is there a way to automatically update my blade with the ...