Angular regular expression that identifies two words enclosed by ->

Does anyone have a regular expression that can match various types of text strings, including empty chains and word combinations?

"" (empty chain)

word1->word2

word1 -> word2

succesives \r\s\t

\n

etc. The words can contain digits, # symbols, or any symbol except \n.

I've tried using the following regex:

const expreg = new RegExp('([^"->"\n]->[^"->"\n])|(([^"->"\n]->[^"->"\n])\n)*');

But it's not capturing all the variations I need, such as

word1->

and it doesn't work for hello1->hello2 either.

((^(\s\r\t|\n|->)^(->|\n)->^(\s|\r|\t|\n|->)^(->|\n))|(^(\s|\r|\t|\n|->)^(->|\n)->^(\s|\r|\t|\n|->)^(->|\n)\n)|(\n)|([\s\r\t]+)|([\s\r\t]+\n))

Answer №1

To match one or more word characters, followed by a repeating pattern of "->" and one or more word characters again.

The entire expression can also match an empty string if needed.

^(?:[^->]+[^\S\r\n]*->[^\S\r\n]*[^->]+)?$

Check out the regex demo

const regex = /^(?:[^->]+[^\S\r\n]*->[^\S\r\n]*[^->]+)?$/m;
[
  "word1->word2",
  "",
  "word1  ->   word2",
  "my django db->my django db",
  "tds->dfds\nmy->computer",
  "word1->",
  "word1  ->   word2 ->word3"
].forEach(str => console.log(`${regex.test(str)} "${str}"`));

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

Getting the current year, month, and day in Angular: a quick guide

I attempted to extract the year, month and day using Angular code, but unfortunately it did not yield the desired outcome. let currentDate = new Date(); Instead of retrieving just the year, month and day values, the current date with time is being displa ...

How to showcase the array object nested within an array of objects in Angular 2

Here is the content of my JSON file: country_id:String, name:String scholardetails:[{ country_id:String, scholarshipname:String, scholarshiplink:String }] ...

Can you explain the concept of System.register in a JavaScript file?

Can you explain the purpose of System.register in a JS file when utilizing directives in Angular 2? ...

Using a Jasmine spy to monitor an exported function in NodeJS

I've encountered difficulties when trying to spy on an exported function in a NodeJS (v9.6.1) application using Jasmine. The app is developed in TypeScript, transpiled with tsc into a dist folder for execution as JavaScript. Application Within my p ...

Stop unnecessary updating of state in a global context within a Functional Component to avoid re-rendering

I am currently working with a Context that is being provided to my entire application. Within this context, there is a state of arrays that store keys for filtering the data displayed on the app. I have implemented this dropdown selector, which is a tree s ...

404 Error message encountered across all routes in a Node TypeScript Azure Function App - Resource not located

I keep encountering a 404 Not Found error on all routes while requesting my API. I am struggling to correctly write the code. Here's an example of my code: host.json: { "version": "2.0", "extensions": { & ...

What is the procedure for retrieving the value of a different column within the columnDefs in an ag-Grid Angular configuration?

I am working with an ag-Grid that has two columns: one for ID and another for Description. The column definitions for these two columns are constructed in the ngOnInit() method within the TypeScript class. In the Description column, I am using cellRendere ...

What is the best way to send an action based on the HTTP method being used?

I've been utilizing NGXS for handling state in my angular project. What would be considered a best practice? Should I make the HTTP call first and then dispatch an action within its subscription? Or should I dispatch the action first and then make t ...

Is it possible to modify parameters in a nodejs express route using Regex? And if so, what is the proper method to address any

When working with Node.js Express, I utilized the following regex pattern: router.get('/test/:name(\\w{1,10}([\-]{1}\\w{0,15}){0,5})', (req, res) => { console.log(c.green, 'It works!'); res.send(&apo ...

Flex's lexical analyzer triggers a lexical error when encountering a space immediately following a token and other elements

While running the lexical analyzer using the provided example, it appears that it is unable to recognize empty spaces between tokens and the tokens generated by the given regex. Lexical analyzer (Something.l) : %{ #include <stdio.h> #include <std ...

Executing External Functions After PayPal Confirms Transaction Completion

As I implement the PayPal Smart Button into my checkout process, below is the code snippet that renders the button: LoadPaypalButton(orderid :string , link : string,token : string , applicationbaseurl:string) { this.addPaypalScript().then(()=>{paypal ...

I've encountered an issue where my React website functions correctly on the development server but not on the live website. Where should I start investigating to find the cause of this discrepancy?

I am trying to embed a Datawrapper map using the following code: import InnerHTML from 'dangerously-set-html-content' export function Map1(){ const htmlFile = `<div style="min-height: 374px"> <script type="text ...

Showing content based on the route - Angular

I'm struggling to hide the navbar based on a specific route in my application. I have managed to subscribe to the route changes, but I am having difficulty changing the display property accordingly. Here is what I have so far: export class AppCompo ...

Create a dedicated file for defining the passport strategy and ensure it is utilized consistently throughout the entire application

I've been grappling with this problem for quite some time now, but I just can't seem to wrap my head around how it functions. There's a file named passport.ts in my project which looks something like this: passport.serializeUser<any, any ...

Creating a customized pagination feature in Angular 8 without relying on material design components

I am looking to implement pagination in my Angular 8 project without relying on any material library. The data I receive includes an array with 10 data rows, as well as the first page link, last page link, and total number of pages. Server Response: { ...

Why does Angular use '?' for optional properties?

While reviewing the HTML template of an Angular application, I came across this syntax that caught my attention. <label>{{header?.title}}</label> I am curious about the purpose of ?.. Can someone explain it to me, please? ...

Encountering errors after updating to Angular Material version 6.4.7

I recently updated my Angular/Material to version 6.4.7 in my Angular2 project, but now I am experiencing a number of errors. I suspect that this may be due to the fact that my Angular/CLI version is at 1.7.4. Is there a way for me to successfully integra ...

Expanding a JSON type in Typescript to accommodate interfaces

Expanding on discussions about passing interfaces to functions expecting JSON types in Typescript, this question delves into the complexities surrounding JSON TypeScript type. The scenario involves a JSONValue type that encompasses various data types like ...

Ways to adjust CSS styles according to the height of various device screens

Is there a way to adjust CSS/SCSS rules in applications like Angular based on the viewport height of various devices? When dealing with viewport width, we can use @media (max-width: ...) {}, but how can we address the height aspect? For instance, how wou ...

Troubleshooting in TypeScript: Difficulty iterating line by line through an uploaded file on Angular 9 platform

I need to loop through a user uploaded file line by line within my Angular application. I attempted the method mentioned in this solution but encountered the following issue: core.js:6260 ERROR TypeError: this.firstfile.split is not a function or its ...