Using JavaScript regex with optional match on either the left or right side

Hello, I am having trouble getting my pattern to work correctly. My goal is to detect if a specific word has any word or letter either on the left side, right side, or both sides.

For example:

  • a{placeholder} = found
  • {placeholder}b = found
  • a{placeholder}b = found
  • {placeholder} = not found

This is the pattern I have so far:

(\w)?\{LINK_TO_WEB_VERSION\}(\w)?

https://regex101.com/r/hX4lM0/1

Answer №1

In order to properly specify the patterns with the delimiter |, you must do so explicitly.

\w\{WEB_VERSION_LINK_TO\}\w?|\w?\{WEB_VERSION_LINK_TO\}\w|\w\{WEB_VERSION_LINK_TO\}\w

SEE EXAMPLE

Answer №2

Utilize this regex pattern with negative lookahead:

/^(?!\B{WEB_LINK}\B).+$/gim

Try the RegEx here

Answer №3

Expand your possibilities

1: Utilize the or condition (|) to match all instances with a letter before or after as shown in this example: /(\w){word}|{word}(\w)/img If one of these conditions is met, there is no need to match before AND after ;)

2: Exclude all occurrences: locate placeholder without a letter before and after it: /[^A-Z]{word}[^A-Z]/img => if not found, any other scenario signifies that it is present

You may opt not to use look behind or look ahead, but they are available if you choose to do so: /(?<!\w)\{word\}(?!\w)/, however, exercise caution as look behind is unsupported by certain languages (e.g. javascript).

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 is the most effective way to organize an array according to a key function that is computationally intensive?

After posting this question regarding sorting an array with a key function, it became evident that utilizing a comparison function was inevitable. The challenge at hand includes: Having a resource-intensive key function that needs to be transformed into ...

In JavaScript, learn how to trigger a statement only when two specific events occur simultaneously

<html> <head> <style> div{ border: 1px solid black; width: 500px; height: 500px; } </style> <script> window.onload = function(){ document.body.onmousedown = function ...

Is there a way to manipulate the triangular side zones on an icosahedron so that they vary in size without altering the overall shape of the object?

Is there a way to create an icosahedron that rotates while the triangular sides increase in size, similar to what is shown in this video: Watch here. Check out this JavaScript code using THREE.js THREE.IcosahedronGeometry = function ( radius, detail ) { ...

Determining the position coordinates of an element in relation to the viewport using JavaScript, regardless of its relative positioning

I am currently developing a vueJs web application and I'm in need of determining the position of an element within my web app relative to the viewport itself, rather than its parent elements. I'm curious if there exists a function or property tha ...

When downloading text using Angular, the file may not display new line characters correctly when opened in Notepad

When downloading text data as a .txt file in my Angular application using JavaScript code, I encountered an issue. Below is the code snippet: function download_text_as_file(data: string) { var element = document.createElement('a') eleme ...

Can I send both a list of files and an entire JSON object in a FormData object to an ASP.NET CORE MVC Controller?

I'm looking for a way to send a formdata object to an MVC controller that includes both a JSON object with nested objects and a list of files. I've attempted to stringify the object into a JSON object, but the controller is unable to read the pro ...

Error: The function exec in matchExpr[type] is not defined

I made some changes to Object.prototype and now I'm running into errors with jQuery's methods on selectors. The error message I'm getting is: Uncaught TypeError: matchExpr[type].exec is not a function Additionally, when trying to use $.po ...

Tips for extracting the index of the chosen item from a dropdown using ReactJs and Material UI

This is the code snippet for Dropdown component implementation: <DropDownField name={formElement.name} label={formElement.name} value={formik.values[formElement.name] || ''} dropDownItems={formElement.name === &apo ...

Pulling data from a MySQL database using AJAX and PHP to convert it into a JavaScript variable, resulting in

I am attempting to retrieve MySQL data using PHP, convert it to JSON, and then pass it into my JS variables for Chart.js. The JSON generated by PHP appears correct. However, when trying to access the data within my variables, the console is displaying them ...

Currently, I am a beginner in the world of reactjs and I have embarked on a practice project to expand my knowledge of the platform. I am in need of assistance in creating a feature that

fetch("https://sampleapi.com/api/v1/employees").then( response => { response.json().then( result => { console.log(result.data); if (result.data.length > 0) { var content = ""; re ...

Dealing with Asynchronous Frustrations: Is it best to utilize callbacks and what is the most effective way to pass them across various

I am working on developing a straightforward text-based game that functions within a socket.io chat room on a node server. The structure of the program is as follows: At present, there are three key modules: Rogue: serves as the core of rogue game functi ...

Removing an element from an object using ng-model when its value is empty is a standard practice

Utilizing the $resource service to access and modify resources on my API has been a challenge. An issue arises when the ng-model-bound object's value is set to empty - the bound element is removed from the object. This results in the missing element ...

issue with adding string to listbox using angularjs

When attempting to add the string "New" to a list box, it is being inserted as "undefined". $http({ method: 'GET', url: 'http://xxx/api/Maintenance/GetAllFilteredItems', params: { Pt_Id: PtId} ...

My function is not working with jQuery's .append method

As a newcomer to javascript and jquery, I am experimenting with adding html through a javascript function. In an attempt to place the contents of variable "y" at a specific location, I am using a div tag with the id of "Contacts" and trying to append elem ...

Try implementing transform translate with absolute coordinates when positioning elements on a webpage

If I wanted to create a box that moves along with the mouse cursor, I could achieve that using the code below. document.addEventListener('mousemove', (e) => { document.documentElement.style.setProperty('--mouse-x', e.clientX ...

Retrieve the value of a selected element

I currently have an HTML select drop down that is populated by a JQuery get request. You can see this in action at this link. My goal is to access the specific piece of code for the selected value every time it changes. <small class="text-muted"> ...

What is the best way to reach the parent controller's scope within a directive's controller?

In a scenario where I have a custom directive nested inside a parent div with a controller that sets a variable value to its scope, like this: html <div ng-controller="mainCtrl"> <p>{{data}}</p> <myDirective oncolour="green" ...

I am experiencing difficulties with displaying my array of JSX elements in the render function of my ReactJS application. What could be

I am currently working on a trivia application and encountering an issue with inserting an updated array of "Choice" elements for each question. Despite my efforts, whenever I attempt to insert an array of JSX elements, the array appears blank. This is qui ...

What is the process of retrieving the converted value from a response using jquery's ajax method?

I have been utilizing the following link: freecurrencyconverterapi in order to retrieve the converted value from USD to INR. If you inspect the developer mode of your browser, you'll notice that the response is {"USD_INR":64.857002}. Since ...

Tips for updating and inserting dynamically generated form textbox values into a MySQL database

In my PHP project, I have a MySQL DB with a bunch of features table. I retrieve the data using PHP and create a table with textboxes named after column names and record IDs in the photos table using the following code: functions.php Code: function m_html ...