Switch out text and calculate the frequency of letters in a given string

I have a string that looks like this: "061801850010300-09/A/B". My goal is to replace all "/" with "-" and also change "A" to "1" and "B" to "2".

My objective is to assign each letter in the alphabet a numerical value - for example, A as 1, B as 2, C as 3... and so on up to Z as 26.

Answer №1

let code = "061801850010300-09/A/B"
.replace(/\//g, '-')
.replace(/[A-Z]/ig, function(c){
   return c.toUpperCase().charCodeAt(0)-64; 
});

Answer №2

If you want to replace specific characters in a string, you can use regular expressions to match them and define how they should be replaced:

input = input.replace(/([\/A-Z])/g, function(m) {
  return m == "/" ? "-" : m.charCodeAt(0) - 64
});

Check out this demo: http://jsfiddle.net/Guffa/h4t56/

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

A guide to selecting the dropdown item labeled as (Select All) using Python and Selenium

edit: Trying to submit parameters for a database-generated report on this page. Successfully modified the start date in the first field using send_keys(), but unable to click on "(Select All)" for fields 3 and onwards, except one. In order to access the h ...

Is there a way to determine the actual time or percentage completion of a file upload using Telerik RadUpload?

Utilizing the Telerik upload file control with manager is a key component of my project: <telerik:RadUpload ID="RadUpload" Runat="server" MaxFileInputsCount="5" /> <telerik:RadProgressManager ID="RadProgressManager" Runat="server" /> For clie ...

The is_date() function is not working properly

I have created a PHP method that validates whether a passed-in parameter is a date. Here is the code: public function validateDate($str){ if (is_numeric($str) || preg_match('^[0-9]^', $str)){ $stamp = strtotime($str); ...

Calculate the total number of pages within an epub document

As a beginner in the world of epub, I have acquired numerous files in different epub formats and now wish to make them easily readable online. I'm not quite sure what an epub file contains. Is there a method to determine the number of pages in my epub ...

"Why does the form.submit() function fail in IE9 when the form is in an iframe and the user is coming from Gmail

I have recently developed a function within my CodeIgniter framework that allows me to send emails with a backlink to my site. The link directs users to a page on my website that includes an iframe. Within this iframe, I have implemented a file input form ...

Tips for transferring the id from the url to a php function seamlessly without causing a page refresh

I have a div that includes a button (Book it). When the button is clicked, I want to append the id of the item I clicked on to the current URL. Then, use that id to display a popup box with the details of the clicked item without refreshing the page, as it ...

Difficulty dealing with Firestore using get() followed by add()

Recently started learning Vue.js and Firestore, facing a challenge with what should be a simple task. 1) I am trying to fetch default values from an existing template document in my Firestore database. 2) The goal is to use these default values to create ...

Encountering an undefined array within a click function nested inside a for loop

Being a newbie in this field, I seem to have overlooked a simple detail. The for loop is functioning correctly, but within it, I encounter an issue with an undefined variable. var categories_info = ["history","excellence","art","social","facilities","p ...

Pattern for extracting a single section from a JSON request using regex

In my current string format, I have the following JSON content: { "group By": "name", "time Period": { "from": "2015-12-29", "to": "2016-02-29" }, "query String": "[nation]: \"India\" AND [education]: \"be", ...

Incorporating dynamic data binding using AngularJS in a span tag

In my view, I am using this Angular expression: <span ng-if="{{list.StoreList ? (list.StoreList.length ' Products)' : '(0 Products)'}}"> </span> The purpose is to display the count of items in StoreList if there are any, ...

Experimenting with a function that initiates the downloading of a file using jest

I'm currently trying to test a function using the JEST library (I also have enzyme in my project), but I've hit a wall. To summarize, this function is used to export data that has been prepared beforehand. I manipulate some data and then pass it ...

Looking to retrieve country, state, city, and area based on inputting a pincode value using Node.js?

I'm currently working on a web project with nodeJs and ejs. I am looking for a solution that can automatically update the country, state, city, and area fields based on the input of a pin-code (zip-code). Are there any recommended packages in node js ...

Tips on ensuring Angular calls you back once the view is ready

My issue arises when I update a dropdown list on one of my pages and need to trigger a refresh method on this dropdown upon updating the items. Unfortunately, I am unsure how to capture an event for this specific scenario. It seems like enlisting Angular ...

Is there a more efficient approach to streamline Javascript code similar to the chaining method used in JQuery?

When working with jQuery, it's possible to streamline the code by running multiple methods in a single statement: $("#p1").css("color", "red").html("Hello world!").attr("class","democlass"); But how can this be accomplished in Javascript? document. ...

Is there a way to incorporate the information from PHP files into the output produced by JavaScript?

I am currently working on a JavaScript script that scrapes data and displays the result on the screen successfully. However, I now face a challenge in wrapping this output with pre and post content from PHP files for formatting purposes. Here is an overvi ...

Executing a Sequence of SQL Queries in Node.js

I am facing the challenge of performing nested queries to retrieve values from the database in order to generate a chart. The current approach involves executing a total of 12 queries, each aggregating the number of customers for every month of the year. ...

How to populate the space beneath the `AreaChart` curve in `recharts` when data includes positive and negative values

How can I modify my chart, created using the recharts library in JavaScript, so that the area under the curve fills to the bottom of the visible area instead of stopping at zero? This is how it currently looks: My goal is to have the curve fill all the w ...

The functionality of my website is currently experiencing difficulties when accessed through the Android UC Browser

My website, , is experiencing issues with product loading and the '+' button in the side menu not working on UC Browser. It works fine on other Android browsers like Chrome and Firefox, but I am confused as to why it is not functioning properly o ...

What is the process for storing user-provided document names in Firestore database entries?

I'm encountering an issue with my API while trying to utilize Firestore for inputting data for login and registration via the API. The problem arises when attempting to add a document entry in the database with the user's input email during regis ...

Export a specifically designed object from a module using Python

When working with node.js in JavaScript, you can set module.exports = 13; in a file called module.js, and then import it elsewhere using x = require("module.js");. This will directly assign the value of 13 to variable x. This method is useful when a modul ...