Enhance regex performance using tokens

Currently, I am using this regex pattern which is functional, but there is room for optimization. The pattern seems very "literal" and may be impacting the processing speed when using tokens. While it could potentially run faster with optimized tokens, the process is not very straightforward for beginners...

const pattern = /left\s:([-]{0,1}[0-9]{1,4})px;\stop:([0-9]{1,4})px;"\s\s\tonmouseover="updi\(event,'([0-9]{4}-[0-9]{2}-[0-9]{2})\s([0-9]{2}:[0-9]{2})\s([A-Z]{3,4})\s\((T[+]{1}\s[0-9]{1}:[0-9]{2}|T[+]{1}[0-9]{2,3}:[0-9]{2})\)<br>Distances:&nbsp;([0-9]{1,4}\.[0-9]{1}nm)\/([0-9]{1,4}\.[0-9]{1}nm)<br><b>Wind:</b>\s([0-9]{1,3}&deg);\s([0-9]{1,2}\.[0-9]{1}\skt)\s\(<b>TWA\s([-]{0,1}[0-9]{1,3}&deg);</b><br><b>Heading:</b>\s([0-9]{1,3}&deg);<b>Sail:</b>\s([a-zA-Z]{2,4})<br><b>Boat\sSpeed:</b>\s([0-9]{1,3}\.[0-9]{2}\skts)/

The regex pattern extracts values that can be utilized later in the code, as shown in the following example:

<img src="img/dot.png" alt="" class="abs" style="z-index: 1; left :-4904px; top:2437px;" 
onmouseover="updi(event,'2019-10-15 02:00 CEST (T+ 1:50)<br>Distances:&nbsp;1271.8nm/447.1nm<br><b>Wind:</b> 295&deg; 5.8 kt (<b>TWA 65&deg;</b>)<br><b>Heading:</b> 230&deg;<b>Sail:</b> Jib<br><b>Boat Speed:</b> 3.23 kts','220px')" onmouseout="cleari()" 
onmousedown="show_wind(366);">
<img src="img/dot.png" alt="" class="abs" style="z-index: 1; left :49px; top:243px;" 
onmouseover="updi(event,'2019-10-15 02:00 CET (T+363:50)<br>Distances:&nbsp;1271.8nm/447.1nm<br><b>Wind:</b> 295&deg; 5.8 kt (<b>TWA 65&deg;</b>)<br><b>Heading:</b> 230&deg;<b>Sail:</b> Jib<br><b>Boat Speed:</b> 3.23 kts','220px')" onmouseout="cleari()" 
onmousedown="show_wind(366);">

Answer №1

Using an HTML parser is a much better approach for solving this issue. If there are even slight differences between your regex and the HTML structure, everything could potentially break. It's not advisable to work with HTML using Regex.

However, if you still prefer optimizing your regex, it can be refined as follows:

  • [0-9] can be replaced with \d
  • [a-zA-Z_] can be replaced with \w
  • [-]{0,1} can be replaced with -?
  • T[+]{1} can be replaced with T\+
  • If text is unimportant, it can often be skipped using .*, provided there is a clear pattern after it

Below is a simplified version of the regex you shared. It may not necessarily be faster than the original one since the provided regex doesn't seem to function correctly. I strongly suggest utilizing an HTML parser instead of relying solely on regex.

left :(-?\d{1,4})px; top:(-?\d{1,4})px;"\s*onmouseover="updi\(event,'(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}) (\w{3,4}) \((T\+ ?\d{1,3}:\d{2})\)<br>Distances:&nbsp;(\d{1,4}\.\dnm)\/(\d{1,4}\.\dnm)<br><b>Wind:<\/b> (\d{1,3}&deg); (\d{1,2}\.\d kt) \(<b>TWA (-?\d{1,3}&deg);<\/b>\)<br><b>Heading:<\/b> (\d{1,3}&deg);<b>Sail:<\/b> (\w{2,4})<br><b>Boat Speed:<\/b> (\d{1,3}\.\d{2} kts)

Test it here!

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

What are some strategies for effectively incorporating anonymous functions in React Router when defining routes?

Why is it necessary to pass props to the anonymous function below? Is using an anonymous function the only way to share props with a component inside a route? function App(props) { const { data } = props; return ( <Router> <Route ...

I am searching for a regular expression in JavaScript that can detect balanced parentheses within a phone number. The pattern I currently have is /^([1]{0,1})s?(?d{3})?[-s]?d{3}[-s]?d{

When entering a phone number, the following format must be followed: The phone number may start with a "1," which is optional. There can be a space after the starting digit, which is also optional. An opening parenthesis "(" is optional. This should be fo ...

What is the best way to remove existing values and update with new values from a JSON using JavaScript?

The following is an excerpt of html code: <div id="prt"> <select name="selectPrt" class="span12" onchange="sta_callPRT();"> <option value="noPrt">Choose one vehicle.</option> <option value="prt1"> ...

Send a REST Request and retrieve a Response without the need to refresh the page

I need to show the result of a REST request without having to refresh the page. Using RAVE for this purpose. Below is the URL for the POST request: JSON data: { "recipientaccount": "0690000034", "destbankcode": "044", "PBFPubKey": "FLWPUBK-4e ...

Making an Ajax request with JSON is yielding unexpected variables that cannot be modified or removed

Attempting to make an AJAX call using a script: $.ajax({ url: pageURL, data: loadData, type: 'POST', cache: false, dataType: 'json', success: function (data) { //if the call was successful console.log(su ...

Custom Notification Prompt Configuration and Notification Button Customization

Is there a way to personalize the default OneSignal Prompt Settings & Notify Button? https://i.sstatic.net/L6FmM.png I am looking to replace the standard notify button with a bell icon and instead use a bootstrap alert containing the message "Do you want ...

Guidelines for exporting and importing data in Sketch.js: converting data into an image, ensuring transparent lines are displayed as opaque

Currently, I am utilizing Sketch.js for the creation of a basic drawing canvas. I encountered an issue when attempting to load an image onto the canvas for inclusion in the drawing process. However, I was able to resolve this by commenting out the line thi ...

Looking for assistance in implementing Chart.js within a modal popup

Here's a Chart-generated diagram: <div class="diagram"> <canvas id="pie" class="chart chart-pie" width="150" height="150" data="ModalObj.FunctionalTest" labels="labels" colours="Colours"> </canvas> This is a directive: .directive( ...

Issue with Jquery .scroll() not functioning in Internet Explorer when using both $(window) and $(document). Possible problem with window.pageYOffset?

Here is the code snippet I am currently struggling with: $(window).scroll(function() { var y_scroll_pos = window.pageYOffset; var scroll_pos_test = 200; if(y_scroll_pos > scroll_pos_test) { $('.extratext').slideDown(&a ...

How to retrieve the name of a node using partial text in JavaScript

I am looking for a way to separate values into key-value pairs before and after a specified delimiter (:) in JavaScript. For example: Product Name: Product 15 Product code: 1234 If I search for "product," I want to retrieve the product name and product c ...

Guide to applying external styles to sub-components in Material UI while maintaining a cohesive common style throughout the entire component

I have a main page that consists of various smaller components. Within the page folder, there is the main page file, an external styles file, and folders for each individual sub-component - each of which has its own unique style. I'm struggling to fi ...

Discover the smallest value in real-time during ng-repeatiteration

In my HTML code, I am utilizing the ng-repeat function. When the user clicks on addBox(), a new input box is appended for adding new data: <div class="col-md-6 col-md-offset-2"> <h5 class="over-title">Variant</h5> <a class="btn ...

Importing three.js using ES6 syntax

When it comes to working with ES6, my workflow involves using Babel and babel-plugin-transform-es2015-modules-system.js specifically to transform module import/export for compatibility with system.js. I rely on a "green" browser for most ES6 features excep ...

Storing files in MongoDB to ensure no duplicates are saved

I'm currently working on a project where I need to save multiple documents in mongodb using mongoose. Additionally, I want to make sure that no duplicates are saved. Here is the function I have implemented: const Stock = require('./models/stock&a ...

Troubleshooting issue with TypeScript: Union types not functioning correctly when mapping object values

When it comes to mapping object values with all primitive types, the process is quite straightforward: type ObjectOf<T> = { [k: string]: T }; type MapObj<Obj extends ObjectOf<any>> = { [K in keyof Obj]: Obj[K] extends string ? Obj[K] : ...

I'm struggling to position a Dojo widget on the webpage

Looking for assistance with a straightforward widget: define(["dojo/_base/declare", "dijit/_WidgetBase", "dojo/dom-construct"], function(declare, WidgetBase, domConstruct){ return declare("gijit.workflow.debug.combi", [WidgetBase], { startup: ...

Troubleshooting problem with Z-Index conflict in Flash animation

I am facing an issue with two HTML divs - one containing a flash movie and the other simple text. I want to place the textual div on top of the flash movie div, but no matter how I set their positions in CSS or adjust their Z-Index values, the text keeps ...

Reverse the color of H1 text within a vertical div

Is it feasible to reverse the coloring of a segment within an h1 element using a div, horizontally? Refer to the illustration shown in the image below. https://i.sstatic.net/QAKwD.jpg ...

Difficulty comprehending/implementing callback functions

After reviewing this discussion and chatting about it, I have come to understand that utilizing a callback function is the most effective approach! "Is using a callback the only solution for acquiring a link from the server via ajax, storing it in a varia ...

jquery fails to update the value of an element

Here is my JavaScript code snippet: <script> $('#DIVID01').text("Loading..."); var Httpreq = new XMLHttpRequest(); Httpreq.open("GET", 'http://httpbin.org/ip', false); Httpreq.send(null); $('#DIVID01').text("Done ...