Exploring the world of Unicode in Angular

I need to search for pokemon and retrieve all Pokémon results (with the accent included)

This is my array:

let games = ['The Legend of Zelda', 'Pokémon', 'Chrono Trigger']

This is how I am attempting to do it:

Using HTML

<input type="text" [(ngModel)]="search">
<ul>
  <li *ngFor="let game of games | filterBy:[]:search">{{game}}</li>
</ul>

I am utilizing the ng-pipes package to implement the filterBy

In TypeScript

public search: string = ''

However, when I enter pokemon, it does not match with Pokémon

Answer №1

Glad to share that I successfully resolved the issue by conducting some tests. I opted to develop a function that utilizes regular expressions to retrieve the desired value.

Below is the outcome:

TS

public games = ['The Legend of Zelda', 'Pokémon', 'Chrono Trigger']
public gamesCopy = this.games

public search: string = ''

private slug(val) {
  val = val.replace(/[áàãâä]/g, 'a')
  val = val.replace(/[ÁÀÃÂÄ]/g, 'a')
  val = val.replace(/[éèêë]/g, 'e')
  val = val.replace(/[ÉÈÊË]/g, 'e')
  val = val.replace(/[íìîï]/g, 'i')
  val = val.replace(/[ÍÌÎÏ]/g, 'i')
  val = val.replace(/[óòõôö]/g, 'o')
  val = val.replace(/[ÓÒÕÔÖ]/g, 'o')
  val = val.replace(/[úùûü]/g, 'u')
  val = val.replace(/[ÚÙÛÜ]/g, 'u')
  val = val.replace(/[ç]/g, 'c')
  val = val.replace(/[Ç]/g, 'c')
  return val.toLowerCase()
}

public searching(search: string) {
  this.gamesCopy = this.games.filter(res => this.slug(res).indexOf(this.slug(search)) > -1)
}

HTML

<input type="text" [(ngModel)]="search" (ngModelChange)="searching($event)">
<ul>
  <li *ngFor="let game of gamesCopy">{{game}}</li>
</ul>

I eliminated the usage of ng-pipes.

That wraps it up :)

Answer №2

For those unconcerned with supporting Internet Explorer, an effective solution is to utilize the normalize method:

<input type="text" [ngModel]="search" (ngModelChange)="onSearchChange($event)">
<ul>
  <li *ngFor="let game of games | filterBy:[]:search">{{game}}</li>
</ul>

onSearchChange(search: string): void {
  this.search = search.normalize('NFKD').replace(/[\u0300-\u036F]/g, '');
}

This approach effectively handles any non-standard characters present in the input.

However, for those who require IE compatibility, alternative methods can be explored by referencing resources such as: Efficiently replace all accented characters in a string?

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

Exploring the Integration of Angular 5 with Firestore for Working with Nested

I have a firestorm collection that follows this specific structure: USERID { email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="354150464175415046411b565a58">[email protected]</a>" name: "John Doe" ...

Customizing Carousel Arrows in Angular with ng-bootstrap

I need help changing the position and icon of control arrows using Bootstrap. I've tried targeting "carousel-control-prev-icon" & "carousel-control-next-icon", but nothing seems to work. Any suggestions on how to properly solve this issue? Here is th ...

Losing scope of "this" when accessing an Angular2 app through the window

My Angular2 app has exposed certain methods to code running outside of ng2. However, the issue arises when calling these methods outside of ng2 as the context of this is different compared to when called inside. Take a look at this link to see what exactl ...

Updating object properties in Typescript

I have written the following Angular 2 TypeScript code: import { Component } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) ...

forwarding within afterCallback employing nextjs-auth0

I need to handle multiple cases for redirecting users based on various fields and custom claims in the user token, which involves navigating through complex if/else blocks. Let's consider a simpler example where I want to redirect the user to /email- ...

Error: Angular version 15 is unable to locate the module '@env/environment' or its corresponding type declarations

Recently, I developed an Angular 15 application with the environments folder located under src. Here is a snippet from my tsconfig.json file: "baseUrl": "./src", "paths": { "@app/*": [ "app/*" ], "r ...

The getStaticProps() method in NextJS does not get invoked when there is a change in

I have integrated my front-end web app with Contentful CMS to retrieve information about various products. As part of my directory setup, the specific configuration is located at /pages/[category]/items/[id]. Within the /pages/[category] directory, you w ...

I am encountering a multitude of errors while trying to run the npm install

Recently, I set up Windows 11 on my new laptop and also installed Node.js and Angular Cli. However, I encountered errors when trying to run npm install in my project. Despite numerous attempts, I have been unable to resolve the issue. I have attempted var ...

The Angular 6 service is not being invoked as expected, as it is not appearing in the network requests either

I am facing an issue with my Angular 6 application while trying to utilize a GET service to retrieve IP information from the server. Despite my various attempts, the GET service is not being executed and does not appear in the network calls either. Below ...

When the request's credentials mode is set to 'include', the 'Access-Control-Allow-Origin' header value in the response should not be the wildcard character '*'

I'm currently in the process of establishing a connection between my Angular application and Node.js using a GraphQL API, utilizing cookies/sessions for authentication purposes. The GraphQL Playground successfully interacts with the API. Here is how ...

Tips for utilizing parameters within SQL Server

Hello everyone! I am new to SQL Server in Azure functions using Typescript. I am currently facing an issue while trying to update a row in the database using declared variables, particularly with VARCHAR types. Strangely, it works fine in the database tool ...

Retrieve predefined values from a TypeScript controller in Stimulus using the default Symfony configurations

Currently, I am delving into the realm of Stimulus using a standard Symfony6 and Encore setup, with the only notable difference being my use of Typescript. As per the guidance in the Stimulus documentation, typed values should be utilized in the following ...

Unable to locate module: The system was unable to find the specified module '@angular-devkit/build-angular/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js'

After encountering an error with Mat Dialog boxes close button not working on IOS devices (refer to Stack Overflow), I decided to downgrade my Angular project from version 14.0.0 to 13.0.0. I followed a solution provided in another Stack Overflow thread. H ...

What is the best approach to include two ng-contents within a single template and manage them using *ngIf?

Presently, I am dealing with an Angular 2 component that is designed to showcase a Bootstrap 3 button group. This particular component has the capability of having a label or standing alone. In order to address this, I opted to utilize two ng-contents con ...

Angular4 provider being integrated into an AngularJS application offers the provider functionality

I have recently migrated my app from AngularJS to Angular4. Now, I want to create a provider in Angular4 and inject it into the app.config in AngularJS. Here is what I currently have: import { UtilsService } from './../utils/utils.service'; imp ...

When subscribing to an Observable of type number or string (union type), NaN values are returned for string inputs

Within a component, I have a public member that is defined as follows: public docId$: Observable<number | string>; This means that the document ID can either be an integer or a string. Upon initializing in ngOnInit, I have the following code snippe ...

Error encountered: YouCompleteMe is unable to locate the necessary executable 'npm' for the installation of TSServer

While attempting to install YouCompleteMe for vim and enable support for Java and Javascript, I followed the instructions provided here. My command was: sudo /usr/bin/python3.6 ./install.py --java-completer --ts-completer Unfortunately, I encountered an ...

Incorporating aws-sdk into Angular 2 for enhanced functionality

I'm currently working on integrating my Angular2 application with an s3 bucket on my AWS account for reading and writing data. In the past, we used the aws-sdk in AngularJS (and most other projects), so I assumed that the same would apply to Angular2 ...

Is it acceptable to use Angular to poll data from Laravel?

I have created a platform where users can stay updated with notifications and messages without the need to constantly refresh the page. Instead of using third-party services like Pusher, I decided to utilize polling to achieve this functionality. Essentia ...

How to use RxJs BehaviorSubject in an Angular Interceptor to receive incoming data

Being a newcomer to rxjs, I grasp most operators except for the specific use case involving BehaviorSubject, filter, and take. I am working on renewing an oauth access and refresh token pair within an Angular interceptor. While reviewing various codes fro ...