Using String.startsWith() may not consistently work when the string includes unicode characters

Currently, I am in the process of creating a proof-of-concept conjugation practice application using Vue.js. One crucial aspect of this application is that when you enter an answer for a conjugation, it compares the input text using String.startswith(). However, a challenge arises when unicode characters are involved. In most cases, the unicode characters you type do not match those stored in the database. This discrepancy becomes evident even in a simple node CLI example where the "ț" character I type appears different from the one in the database, which is "ţ".

Below is an illustration of the typed input, its value, and unicode value compared:

input: anunț // anun\u21B
comparison: anunţ // anun\u163

I have experimented with methods like .normalize() but unfortunately, it seems that neither the inputted string nor the comparison string is affected by it.

> var input = 'anunț'
> var comparison = 'anunţ'
> input === comparison
false
> input.normalize() === comparison
false
> input.normalize() === comparison.normalize()
false
> input === comparison.normalize()
false
/// etc etc with NFC, NFD, NFKC, NFKD forms
> input.normalize()
'anunț'
> comparison.normalize()
'anunţ'

// i've also tried .normalize() with the string decoded into unicode

I attempted converting to unicode and manually replacing one set of strings, but this method has limitations, including issues such as difficulty in making positive comparisons until the entire string is entered.

Exploring regex comparisons was my next step, although I suspect it might lead me down another complex path.

At its core logic, without any previous attempts, here is what I aim to achieve:

if (this.conjugation.startsWith(this.input)) {
    this.status = "correct";
} else {
    this.status = "incorrect";
}

if (conjugation === val) {
    // okay, we are done
}

Any suggestions on how I could overcome this hurdle? Currently, I am focusing on testing with Romanian verbs, so the characters seem to fall within the following unicode ranges:

\u0000-\u007F, \u0180-\u024F, \u0100-\u017F

Answer №1

To focus on specific differences, you can utilize Intl.Collator in JavaScript:

var word1 = "anunț"; // anun\u21B
var word2 = "anunţ"; // anun\u163

var collator = new Intl.Collator("ro", { sensitivity: "base" });

console.log(word1 === word2); // the words are not identical
console.log(collator.compare(word1, word2) == 0); // ... but they are considered "equal enough"

Answer №2

Although sharing similarities, these two characters exhibit subtle differences. One features a space between the t and the lower comma mark, almost blending into the symbol.

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

I am unable to log in using bcryptjs, but I have successfully been able to register a

Hey there! So I'm diving into Nodejs and I've managed to create a simple login/register API. For password encryption, I'm using bcryptjs. Testing it out on postman, I can successfully register a new user. However, when attempting to login wi ...

Creating a Vue application utilizing a function with an unspecified purpose

Looking to create an app with a function that is currently undefined. On the production server, there is a function called __doPostBack which I am calling from my Vue app like this: getLabel(templateName) { __doPostBack(templateName, ""); } Afte ...

Troublesome GSP: JavaScript not functioning properly in Gr

I am experiencing an issue with the JavaScript code I have. Here's my code: <script type="text/javascript"><!-- ​$(function () { $('#add').click(function() { $(this).before($('select:eq(0)').clone()); ...

Aligning the ant design carousel in the center of the page

I adjusted the size of an Ant Design carousel and now I want to position the image in the center of my screen. This is how the current image looks: https://i.sstatic.net/TmfDb.png Here is what I have attempted: const contentStyle = { heigh ...

Transferring information from a service to an AngularJS controller

I have a service that retrieves data from a URL provided as a parameter, and it is functioning correctly. However, when attempting to pass this data to a controller's $scope in AngularJS, I am not receiving any data. var app = angular.module("Recib ...

The majority of my next.js website's content being indexed by Google consists of JSON and Javascript files

I’m facing an issue with Google indexing on Next.js (utilizing SSR). The challenge lies in ensuring that .HTML files are effectively indexed for SEO purposes. However, it seems that Googlebot predominantly indexes JSON and JavaScript files. To illustra ...

Identifying the hashKey and selected option in a dropdown menu

My attempt to set the selected option for the select menu is not working because the data in the ng-model that I am sending has a different $$hashKey compared to the data in the select menu, and the $$hashKey holds the values. <select class="form-contr ...

Labeling with the index number of arrays

var arr = []; $('.inp').click(function(){ function arrcall() { $.each(arr, function(key, value) { alert('Array Key is: ' + key + ' \n Array Key value is: ' + value); }); } ...

What is the best way to retrieve the text when it is no longer within its original div?

Is it possible to use Javascript to detect when text is flowing out of its containing <div>? For instance, if a text consists of 3 sentences and the last sentence is only partially displayed, can JavaScript be used to capture the entire last sentence ...

Utilize JavaScript, jQuery, or Angular to incorporate identifications into <p> elements

I am working with a dynamically generated HTML document that contains several <p> tags with text inside. My goal is to be able to select a specific <p> tag when a user clicks on the text within it. However, I am restricted from adding ids to th ...

The JQuery(document).ready function does not seem to be executing on the webpage, but it works as expected when placed in a

I have encountered a peculiar problem. It's strange to me because I can't figure out the root cause of it, despite trying everything in the Chrome Developer Tools debugger. Here is a snippet of code that works when I run it from a file on my desk ...

A guide on extracting/filtering information from JSON files using JavaScript

Is it possible to extract specific data from a JSON file using JavaScript? I have imported a JSON file into my local JavaScript and I am having difficulty in retrieving and displaying certain information. Could someone offer assistance with this? The JS ...

Unable to start initial Cucumber+javascript demonstration

I attempted to run the initial example provided in the official documentation found here. Using Windows 7x64 bit and node.js version 6.11 I ran the following commands but encountered the same outcome. * node_modules/cucumber/bin/cucumber.js autotests/c ...

The choice between using `npm run serve` and `serve -s dist` in Vue CLI

I have recently developed a Vue app using Vue CLI 3. When I run the command npm run serve in VS Code, the application is successfully displayed at http://localhost:8080. I then proceed to serve the 'dist' folder using the following commands: npm ...

`What is the best way to employ the Return statement in programming?`

Trying to grasp the concepts of functions and methods has been a challenge for me. I often find myself confused about when, where, and how to use return statements in different situations. To illustrate this confusion, let's take a look at two code sn ...

Modify code on click using JavaScript

Here is some code I found for a custom style switcher: I am thinking about integrating it into my bootstrap dropdown button. The current code for the style switcher is as follows: <form id="switchform"> <input type="radio" name="choice" value= ...

Attempting to implement firebase configuration within my Vue component

Edit: I made an error here ^^ !https://i.sstatic.net/0QGRx.png firebase.js?5b23:11 Uncaught TypeError: app.database is not a function at eval (firebase.js?5b23:11) at Object../src/components/firebase/firebase.js (app.js:3496) at __webpack_require__ (app.j ...

The (window).keyup() function fails to trigger after launching a video within an iframe

Here is a code snippet that I am using: $(window).keyup(function(event) { console.log('hello'); }); The above code works perfectly on the page. However, when I try to open a full view in a video iframe from the same page, the ke ...

Ways to widen the header to fit the entire page?

I'm having trouble stretching my header to fill the entire page. I've tried using margin-left and right, but it's not working as expected. My Header CSS: background: green; height: 70px; width: 100%; display: flex; just ...

What could be causing Vue.js to malfunction when attempting to run it?

Having trouble starting my Vue.js application. Encountering the following error: C:\xampp\htdocs\crm-front>npm run dev > <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fd9e8f90d09b8f929389bdccd3cdd3c ...