Discovering the final pattern using regular expressions

var inputString = 'a.b.c.d.e.f';
var pattern = inputString.match(/([^\.]+)\.[^\.]+$/)[1];
console.log(pattern);

I have successfully implemented the code using regular expressions, but I am open to exploring more efficient solutions for extracting the last two strings separated by a DOT (.) character.

Answer №1

To find the last characters separated by a dot, you can utilize a regular expression in your code.

/[^.]+\.[^.]+$/
  • [^.]+ would match a single character not included in the list below

    Quantifier: + This means between one and unlimited times, as many times as possible, with a greedy approach

    . represents the literal character .

  • \. is used to match the character . literally

  • [^.]+ would match a single character not included in the list below

    Quantifier: + This means between one and unlimited times, as many times as possible, with a greedy approach

    . represents the literal character .

  • $ asserts the position at the end of the string

console.log('a.b.c.d.e.f'.match(/[^.]+\.[^.]+$/, ''));
console.log('zz.yy.xx.ww.vv.uu'.match(/[^.]+\.[^.]+$/, ''));
console.log('number with.dot'.match(/[^.]+\.[^.]+$/, ''));

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

Respond to onClientClick based on the button choice made by the user

In my code, there is an OnClientClick event set up like this: OnClientClick="return getErrors();". This function contains the following body: function getErrors() { var errorString = "some errors"; return $('<div id="dialog-message" title= ...

React JS functionality does not support Bootstrap tooltips

I'm attempting to implement a tooltip in my React app, but I'm experiencing issues with it not displaying properly. I am utilizing vanilla Bootstrap for this purpose. I've included the following script tag in my index.html file to import the ...

What is the best way to identify when the soft-keyboard is hidden in the Android browser

Having trouble with the Android Webkit browser and need to manually detect when the soft-keyboard is hidden by pressing the button in the top right corner. https://i.stack.imgur.com/x11Vp.jpg In the image above, pressing the button hides the soft keyboar ...

Creating collections in a Hashtable style using JavaScript

Creating a collection in JavaScript can be done in the following way: Start by initializing an empty collection with var c = {}; Next, you can add items to it. After addition, it will look like: { 'buttonSubmit': function() { /* do some work * ...

Efficiently configuring an Array with Wordpress REST API in JavaScript

Here's a two-part question: 1) My goal is to fetch elements from WordPress' REST API and store them in an array called map_locations. The first part of the function involving $.ajax works as intended, showing data when I log it. However, the lat ...

Is it possible to replicate the dynamic depth effect of the cards on music.google.com using Angular Material?

Upon hovering over the "day of week music station cards," I observed that they gain depth or height. Can the same effect be achieved with Angular Material Design for cards? Please provide a demo code as an example. ...

What is the best way to preserve changes made to DOM manipulation?

Within my controller code, there is a section that removes a specific DOM element: MetrofficeApp.controller('EmployeesCtrl', function($scope) { ... angular.element(deleteElem).remove(); $scope.$apply(); However, when I navigate away from the pa ...

The jQuery ajax function is failing to return any results

Here is the code snippet I am working with: $("#MainContent_btnSave").click(function () { if (($("#MainContent_txtFunc").val() == "") || ($("#MainContent_cmbLoc").val() == "")) { alert("Please make sure to fill in all required ...

Creating TypeScript Classes - Defining a Collection of Objects as a Class Property

I'm trying to figure out the best approach for declaring an array of objects as a property in TypeScript when defining a class. I need this for a form that will contain an unspecified number of checkboxes in an Angular Template-Driven form. Should I ...

Develop a constructor that can be injected

Delving into the world of AngularJS as a beginner, I am starting to grasp the intricacies and distinctions between factory, service, and controller. From my understanding, a factory serves the purpose of returning a "value object" that can be injected. Mos ...

Creating routes in Node.js after setting up middleware

Currently tackling a project using node.js and encountering a specific issue. After setting up all routes with express (app.get("..", func)), I find myself stuck with a middleware that catches all requests and redirects to a 404-page. The problem arises w ...

styles.css is generating an ERROR message, indicating that it is unable to read properties of null when trying to access the 'classList'

Hey there, I've been working on adding a background color change to my navbar when the scrollY exceeds 30px, and it's functioning properly. However, I'm encountering an error in the console which states that the new classList cannot be added ...

Creating a notification for specific choices in a dropdown menu

I am working on a form that includes a select element with multiple options. Depending on the option selected, a different form will be displayed. My concern is if a user starts filling out one form and then decides to select another option, I want to add ...

Update the second dropdown automatically based on the selection in the first dropdown menu

I need assistance with creating two dropdown menus that are linked, so when an option is selected in the first menu, it automatically changes the options available in the second menu. Both menus should be visible at all times. I have set up a fiddle to pr ...

Utilizing commas in JavaScript

Hi everyone, I'm having an issue with printing the message "Invalid password, Must Contain:". The problem I'm facing is that when I write the code in JavaScript, the comma is being interpreted as an operator instead of a regular English comma. Th ...

What is the importance of utilizing clearInterval to restart the timer in ReactJS?

Consider the code snippet provided below: useEffect(() => { const interval = setInterval(() => { setSeconds(seconds => seconds + 1); }, 1000); return () => clearInterval(interval); }, []); What is the purpose of returning ...

The containerElement is not compatible for routing when using a menuItem in React JS

Greetings, I am fairly new to working with React.js and I am currently exploring how to implement routing using material-ui MenuItem. However, I seem to be encountering some difficulties when trying to utilize the ContainerElement feature upon clicking on ...

The regular expression retrieves three pieces of information from the specified range of characters: abcd efhg (abcd)

There is a string available: [18d03] Complete screen 12" (tablet) The goal is to extract the following values: $one = '18d03'; $two = 'Complete screen 12"'; $three = 'tablet'; The current attempt involves using the followi ...

Efficiently loading Angular modules using lazy loading with ES6 and systemjs.import

Currently, I am attempting to establish a base route and implement lazy loading for separate modules using angular resolve alongside system.load. My setup involves leveraging jspm in conjunction with the ES6 module loader. The configuration for the base r ...

What are the key distinctions between an arrow function, a class, and a traditional function?

Is there a way to distinguish between the following three elements in ES6 using its reference? let x = i => i+1; class y { constructor(i) { this._i=i+1; } get i(){ return this._i;} } function z(i) { return i+1; } For example: test(x) //=> ' ...