Indicate the end of the row in JavaScript when using the match() function

What's the best way to identify the end of a row when using the match() function? I'm trying to extract "2021 JANUARY 18 MONDAY" from this page.

https://i.sstatic.net/bTNdQ.png

https://i.sstatic.net/YzFs2.png

If there is additional text after the desired string, I typically use this code:

var content = UrlFetchApp.fetch(url).getContentText();

var date = content.match(/results of (.*?)SOMETHING/m)[1];

However, I am currently facing some difficulties with this method.

Answer №1

This is a simple example demonstrating how to match a specific date format within a string:

let text = "lorem ipsum 2021 January 18 Monday";
let matchingDate = text.match(/(\d{4} [A-z]+ \d{1,2} [A-z]+)/)[0] || null;

console.log(matchingDate);

Answer №2

If you want to ensure the date at the end of the row matches, consider using the anchor character "$" and refining the format for the date pattern.

It's important to note that the regular expression [A-z] actually matches more than [A-Za-z]. To make the pattern case insensitive, you can include the flag /i.

^.* ((?:19|20)\d{2} [A-Z]+ (?:0?[1-9]|[12]\d|3[01]) [A-Z]+)$

Check out a demo of this regex here.

  • ^ Denotes the start of the string
  • .* Matches any character 0 or more times until the last space
  • ( Captures group 1 which contains the date value
    • (?:19|20)\d{2} Matches a year starting with 19 or 20 followed by 2 digits and a space
    • [A-Z]+ Matches one or more uppercase letters A-Z followed by a space
    • (?:0?[1-9]|[12]\d|3[01]) Matches a day from 1 to 31
    • [A-Z]+ Matches one or more uppercase letters A-Z followed by a space
  • ) Closes group 1
  • $ Denotes the end of the string

var page = "NHL 2020-2021 ratings through results of 2021 JANUARY 18 MONDAY";
var date = page.match(/^.* ((?:19|20)\d{2} [A-Z]+ (?:0?[1-9]|[12]\d|3[01]) [A-Z]+$)/i)
if (date)
  console.log(date[1]);

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

The Node.js application is unable to locate the source file path

Currently, I am in the process of creating a simple quiz application. While working on this project, I encountered an issue with linking a JS file in an HTML template. Even though I have confirmed that the path is correct, every time I run my node app, the ...

Accessing a jstl variable within javascript script

I need to access a JSTL variable within a JavaScript function. The JavaScript code submits a form. $("#userSubmit").on('submit', function () { document.getElementById("userForm").submit(); }); In the server-side code - request.setAttribu ...

Having trouble with the clear button for text input in Javascript when using Bootstrap and adding custom CSS. Any suggestions on how to fix

My code was working perfectly until I decided to add some CSS to it. You can view the code snippet by clicking on this link (I couldn't include it here due to issues with the code editor): View Gist code snippet here The code is based on Bootstrap. ...

Converting a Javascript array to an NSArray in Xcode

As a complete beginner to Xcode and programming in general, I recently built an app using javascript and html and integrated it into Xcode. Currently, my main focus is on extracting multiple arrays from the html/javascript file and exporting them as a csv ...

What is the best approach for managing Promise rejections in Jest test scenarios?

Currently, I am engaged in a node JS project where my task is to write test cases. Below is the code snippet that I am working on - jest.mock('../../utils/db2.js') const request = require('supertest') const executeDb2Query = require(&ap ...

What sets local Node package installation apart from global installation?

My curiosity sparked when I began the process of installing nodemon through npm. Initially, I followed the recommended command and noticed the instant results displayed on the right side of my screen: npm i nodemon This differed from the installation ins ...

I need help converting the "this week" button to a dropdown menu. Can someone assist me in troubleshooting what I am missing?

Seeking assistance with customizing the "this week" button on the free admin dashboard template provided by Bootstrap 4. Looking to turn it into a dropdown feature but unable to achieve success after two days of research and watching tutorials. See code sn ...

Stop the scrolling behavior from passing from one element to the window

I am facing an issue with a modal box window that contains an iframe. Inside the iframe, there is a scrollable div element. Whenever I try to scroll the inner div of the iframe and it reaches either the top or bottom limit, the browser window itself start ...

Describing how to assign multiple variables in a VUEX mutation

store.js import Vue from 'vue'; import Vuex from 'vuex'; import userStore from './user/userStore.js'; import VuexPersist from "vuex-persistedstate"; Vue.use(Vuex) const debug = process.env.NODE_ENV != ...

Error in Typescript: The type 'Element' does not have a property named 'contains'

Hey there, I'm currently listening for a focus event on an HTML dialog and attempting to validate if the currently focused element is part of my "dialog" class. Check out the code snippet below: $(document).ready(() => { document.addEventListe ...

The error code 405 (Method Not Allowed) occurs in Ajax when the action field is empty or identical to the current page

Special thanks to @abc123 for sharing the code below in one of their posts: <!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> </head> <body> <form id="formoid" a ...

Switching a jQuery AJAX Response

Currently, I am utilizing an AJAX function to retrieve and display specific categorical posts when a corresponding button is clicked: <script> // Brochure AJAX function term_ajax_get(termID) { jQuery("#loading-animation").show(); ...

Count the number of items in a JSON array in AngularJS with a specific ID

To get the total count of articles in a JSON array, I use the following method: Home.html <div ng-controller="pfcArticleCountCtrl">Number of Articles {{articlecount.length}} items</div> Controllers.js // Calculate total number of articles p ...

discord.js: Imported array not displaying expected values

I've been facing an issue with accessing elements from an imported array. Even though the array is successfully imported, attempting to access its elements using [0] results in undefined. Here's how I exported the array in standList.js: exports. ...

Instructions for implementing tooltips on a pie chart slice when hovering with the mouse pointer, using the canvas

var canvas = document.getElementById("canvas"); var ctx = canvas.getContext("2d"); var cw = canvas.width; var ch = canvas.height; ctx.lineWidth = 2; ctx.font = '14px verdana'; var PI2 = Math.PI * 2; var myColor = ["Gr ...

I'm interested in exploring whether p5.js allows for the creation of a class that can draw sub-classes within itself. One idea I have in mind is to create a 4x4 grid composed of individual

My goal is to create a game similar to Tetris, where the pieces are composed of smaller blocks that share attributes. My current progress includes: export class SquareTetromino { [x: string]: any; constructor(x, y, w, h) { ... } ...

Having trouble getting the Angular 2 quickstart demo to function properly?

Just starting out with Angular 2, I decided to kick things off by downloading the Quickstart project from the official website. However, upon running it, I encountered the following error in the console: GET http://localhost:3000/node_modules/@angular/ ...

Store additional data with the visitor ID WEB in Fingerprint JS V3

After browsing through similar questions, I couldn't find a solution that fits my needs. Basically, I have a website where a random emoji is generated and stored in local storage. However, this method is not effective as users can easily delete their ...

Executing a cloud function in Firebase from an Angular-Ionic application by making an HTTP request

I am a newcomer to GCP and app development, so please bear with me if this question seems mundane. Currently, I have an angular-ionic app that is connected to Firebase allowing me to interact with the Firestore database. Now, my challenge is to invoke a ht ...

Tips for extracting HTML entities from a string without altering the HTML tags

I need assistance with removing HTML tags from a string while preserving html entities like &nbps; & é < etc.. Currently, I am using the following method: stringWithTag = "<i> I want to keep my ->&nbsp;<- element space, bu ...