Regular expressions can be used to target a specific sentence that contains multiple words

With this regular expression, I am able to choose a sentence based on a single word:

[^.]* word [^.]*\.

Is there a way to select a sentence with two or more words conditionally?

For instance, how would I go about selecting a sentence if word A and either word B or word C are present?

Assume that word A = "findings", word B = "support", word C = "research". In this scenario, the sentence below would be chosen as both conditions are met:

Their research findings strongly support the theory proposed in the study.

However, the sentence below would not be selected since it only contains one of the specified words:

Results showed conclusive evidence regarding the impact of their research findings on the community.

Answer №1

I would recommend using the following regex pattern:

\bworda\b[^.!?]*\b(?:wordb|wordc)\b

This regex will identify a sentence if it includes worda, along with either wordb or wordc, without being separated by punctuation marks.

If you prefer to match the words in any order, you can use this regex pattern:

^(?=[^.!?]*\bstudies\b)(?=[^.!?]*\b(?:suggest|evidence)\b)[^.!?]*\.$

var test = [
'Our studies suggest that ACTN3 R577X genotype is a modifier of clinical phenotype in DMD patients.',
'Moreover, studies of the RR genotype have shown a downward linear trend with increased incidences of ankle sprain.',
'Moreover, studies of the evidence of ankle sprain.',
'Moreover, evidence of the studies of ankle sprain.',
'Moreover, the incidences of ankle sprain.'
];
console.log(test.map(function (a) {
  return a+' :'+/^(?=[^.!?]*\bstudies\b)(?=[^.!?]*\b(?:suggest|evidence)\b)[^.!?]*\.$/i.test(a);
}));

Answer №2

One possible solution:

\b\[a-zA-Z]+\b(?=\s*(test|example))

\b represents a word boundary, enabling a search limited to whole words.

[a-zA-Z] will match any word (consisting of one or more characters).

(?=) serves as a lookahead.

(test|example) will return true if it finds either test or example.

See it in action at this regex101 link.

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

How come my date computed property does not update reactively when changes occur?

I have a Date object in my data, and I need to convert the date into a string for a date picker component in Vuetify. The initial date is being read and displayed correctly. I am able to set the date as well - when I set a code breakpoint, I can see the ...

rearranging items from one list to another list

I've encountered an issue in my HTML code that I need help with. I have included some classes for clarity, but their specific names are not essential. My goal is to make the "sub-nav" element a child of the "mobile parent" list item on mobile devices. ...

Tips for extracting the href value in Jest test when using React Router:

Exploring React JS for the first time and currently working on creating a test case using Jest to verify the link in my component. I am utilizing react-router and within the link, I have used <LINK to="{{pathname: link}}" />Link</LINK>. My vers ...

The error message "Cannot construct apickli.Apickli" is indicating a Type Error

Encountering this issue : TypeError: apickli.Apickli is not a constructor every time I attempt to execute Sendpostrequest.js again in the following step of a scenario. In A.js, the initial call to Sendpostrequest works without any problems. However, ...

Avoid having Vue CLI delete all files in the dist folder

As I work on my Vue project, I am facing a challenge in syncing the dist folder with Git. Previously, this process ran smoothly when using webpack. However, after transitioning to @vue/cli and creating my project with vue create myProject instead of vue in ...

Is it possible to determine which child element is currently in view within a scrollable parent div?

In an attempt to replicate a "current page" feature using divs, similar to a PDF reader. document.addEventListener("DOMContentLoaded", function(event) { var container = document.getElementById("container"); container.onscroll = function() { let ...

What is the best way to utilize multiple models under a single root in ReactJS?

Greetings! I currently have a root structure in the following code snippet: <body> <noscript>You need to enable JavaScript to run this app.</noscript> <button >Platform</button> <button >Game</button> < ...

Obtaining a URL from a parameter

I have a unique situation with one of my parameters in the routing, as it involves an actual URL. router.get('/api/sitemap/:url', function(req, res) { var url = req.params.url; ... } How can I ensure t ...

What is the best way to immediately update the state in a React functional component?

Having recently started learning React, I find myself struggling to understand the lifecycle of a functional component. Let's consider a scenario where I have multiple checkboxes labeled as: checkbox, a, b, c, and d, each corresponding to values a, b, ...

showing text from chosen selection with an additional element included in the output

I could use some assistance with this code snippet. My goal is to showcase the options in the format: "Fried Rice = 10.000" as the outcome. However, the issue I am facing is that the select option box also contains the price. What I actually need is for th ...

Determining object types based on sibling properties in Typescript

Can you define properties of an object based on another property within the same object? How do I determine the types for values based on the value of the type property? type StepKeys = "Test1" | "Test2"; interface objectMap { &qu ...

Show the login page first before allowing the comment to be submitted in PHP

My objective is to show the login page before allowing a user to submit a comment. If the user is not logged in and tries to click on the submit button, they should be redirected to the login page. However, if the user is already logged in, the comment s ...

Problem encountered with modal and JavaScript: Uncaught TypeError: Unable to retrieve the property 'classList' of null

Trying to implement a modal feature for the first time but encountering an error when clicking the button. Unsure if I'm following the correct procedure as a JS beginner. Any assistance would be appreciated. ERROR { "message": "Uncaugh ...

Triggering Events with Angular Checkboxes

I'm having trouble getting a console log to trigger when the user checks one of the checkboxes. The script is working fine for everything else, but checkbox clicks are not registering any events. Any suggestions on why this might be happening? (Just ...

Converting a string to a date in JavaScript

Is there a way to convert a string like "20150415" into a date with the format "2015, April, 15th"? I've come across multiple examples, but they all involve splitting with "-" or "/", which doesn't work for my case. Below is my current code snip ...

Adding an item to a JSON array in Angular

I'm having trouble adding a new item to the roleList JSON array. I've tried using push/concat, but it doesn't seem to update the roleList array. Any suggestions on how to resolve this? // The javascript : function RoleListCtrl($scope) { ...

Display the div element only when it is positioned in the center of the page

I am looking to create a smooth fade-in effect for a div when the user scrolls away from the top of the page, but I want the div to be hidden again as they approach the bottom of the page. Specifically, I would like the div to remain hidden when it is wit ...

The JSON information is not appearing as expected in a table that was created using JavaScript

Within my JavaScript code, I have a variable that holds data from PHP like this: var myData = <?php echo json_encode($json_array) ?>; I am attempting to populate a dynamically generated table with the keys and values from this object. However, whe ...

What is the best way to print HTML code without having to write it multiple times?

I am working at a web application and i am doing some refactoring. Doing so, anyway, i was caught in a dilemma: i have some similar nor identical parts in my pages that i want to compress in just one in order to make some edits just once, since edits are ...

Spotting the Visible Element; Detecting the Element in

Currently, my webpage has a fixed header and I am facing the challenge of dynamically changing the styling of the li elements within the navigation based on the user's scrolling position. To achieve this, I believe the most effective approach would b ...