Using regex, match any word along with the preserved white space and consider parentheses as a single word

I need help with creating a regex pattern to split a string of words in a specific way.

The current pattern I've been using is (?!\(.*)\s(?![^(]*?\)), but it's not giving me the desired outcome.

It's close, but not quite there yet.

let message = 'The quick    brown fox (and friend) (jumps over    the) lazy     dog.'
let result = message.match(/regex/g);

Result:

[
 'The', 
 'quick', 
 '    ', 
 'brown', 
 ' ', 
 'fox', 
 ' ',
 '(and friend)',
 ' ',
 '(jumps over ( bonus regex - only captures   the most outer parent parentheses) the)', 
 ' ', 
 'lazy', 
 '     ', 
 'dog.'
];

Special attention should be given to preserving white space and treating long sentences within parentheses as single array items.

Despite finding similar questions, I struggle to adjust the answers to fit my needs due to variations in regex solutions. I'm really bad at regex overall.

Any assistance on this matter would be greatly appreciated!

Edit - Remembered another scenario where consecutive parentheses are present in the string.

Answer №1

Here is a solution that should meet your needs:

(\((?:[^)]*)(?:[^(]*)\))|([\w.,'"!?\\-]+)|(\s+)

// Captures anything within parentheses
// Matches ( followed by anything that is not ), then anything that is not (
(\((?:[^)]*)(?:[^(]*)\))

// Recognizes words with punctuation
([\w.,'"!?\\-]+)

// Identifies any whitespace
(\s+)

This method should only work if the parentheses are balanced pairs. Once matched, you can cycle through the groups accordingly.

Answer №2

below is the regex pattern that will be utilized /(\(.*\)|\s+)/. The explanation of this pattern is as follows ...

  1. /( ... )/ ... captures any match like ...
  2. either ... \(.*\) ... an opening parentheses followed by any characters until the last closing parentheses ...
  3. | ... or ...
  4. \s+ any sequence of whitespace.

const sample = "The quick    brown fox (jumps over ( bonus regex - only captures   the most outer parent parentheses) the) lazy     dog."

const regXSplit = (/(\(.*\)|\s+)/);


console.log(
  'almost the result that was ask for ...',
  sample.split(regXSplit)
);
console.log(
  'the exact result that was ask for via additional filtering ...',
  sample.split(regXSplit).filter(str => !!str)
);

console.log(
  'test viability of both results ...',
   (sample.split(regXSplit).join('') === sample),
   (sample.split(regXSplit).filter(str => !!str).join('') === sample)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

The method below considers the new criteria for approval. It incorporates a basic form of parsing logic to handle both types of parenthesis-enclosed elements, sequential and nested ones ...

const sample = `The quick    brown fox (and friend) (jumps over the) (lazy)     dog.

The quick    brown fox (and friend) (jumps over ( bonus regex - (only captures   the most outer) parent parentheses) the) (lazy)     dog.`;

function parseWordsParenthesisAndWhitespaceSequencesAsEntities(text) {
  function collectOrConcat(collector, token) {

    let { nestingCount, concatenated, list } = collector;

    if (regXLeadingOPT.test(token)) {
      ++nestingCount;
    }
    if (regXTrailingCPT.test(token)) {
      --nestingCount;
    }
    if (nestingCount === 0) {
      if (concatenated !== '') {

        list.push(concatenated + token);

        concatenated = '';
      } else {
        list.push(token);
      } 
    } else {
      concatenated = (concatenated + token);
    }
    collector.nestingCount = nestingCount;
    collector.concatenated = concatenated;

    return collector;
  }
  const regXLeadingOPT = (/^\(/);   // - leading opening parentheses.
  const regXTrailingCPT = (/\)$/);  // - trailing closing parentheses.
  const regXSplit = (/(\([^)(]+\)|\s+)/);

  return text
    .split(regXSplit)
    .filter(str => !!str)
    .reduce(collectOrConcat, {

      nestingCount: 0,
      concatenated: '',
      list: []

    }).list;
}

console.log(
  'demonstration of how the new split regex functions ...',
  'break down into smaller tokens, filter out empty strings and hope for the best ...',
  sample.split(/(\([^)(]+\)|\s+)/).filter(str => !!str)
);

console.log(
  'implementation of minimal *parsing* logic for consecutive and nested parenthesis ...',
  'parse words enclosed within parentheses and sequences of whitespace as entities  ...',
  parseWordsParenthesisAndWhitespaceSequencesAsEntities(sample)
);

console.log(
  'validate effectiveness of final parsing approach ...',
   (parseWordsParenthesisAndWhitespaceSequencesAsEntities(sample).join('') === sample)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

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

React: Modifying state does not update useState array

The state of the array does not change when the state change method is called : const [arrayOfDocuments, setArrayOfDocuments] = useState([]); I have tried : setArrayOfDocuments(...[]); or setArrayOfDocuments([]); Where I use my method : const pushToArr ...

Anticipate feedback from Python script in Node.js

I have developed a website using nodejs and express, but I am facing an issue with integrating a python script for face recognition. The problem lies in the fact that when I invoke this script from nodejs using child-process, it takes around 10 to 20 secon ...

Executing code asynchronously and handling callbacks using Process.nextTick and Promise

When I execute the following code: process.nextTick(() => console.log(8)) Promise.resolve("hi").then(t => console.log(t)) console.log(7); The output displayed is 7 8 hi This behavior is as expected because process.n ...

Looking for a unique search object specifically designed for mongodb?

I am currently developing my first application using node.js and angular, and I have encountered a challenge that I am struggling to solve. Let's say I have a User Schema like this: User = { firstname: "Bryan", lastname: "Allan", email: "<a ...

Transferring data between different elements in a React application

I am currently learning React and facing some challenges in terms of passing data between components. Even after reviewing various tutorials and blogs, I am still struggling to make things work. Within my project, I have two child components named Body-c ...

Ways to incorporate radio buttons, checkboxes, and select fields into a multi-step form using JavaScript

In the snippet below, I have created a multi-step form where users can provide details. The issue is that currently only text input fields are being accepted. The array of questions specifies the type of input required for each question: Question no.1 req ...

Executing a time-consuming function call within the componentDidMount lifecycle method of a React component

When working with my React component, I utilize the componentDidMount function to pass a string received through props to a processing function. This function then returns another string which is used to update the component's state. import React, { C ...

What is the best way to fetch the data from this API?

function fetchCoinPrice(coinName) { return axios .get( `https://min-api.cryptocompare.com/data/pricemulti?fsyms=${coinName}&tsyms=EUR` ).then((response) => (response.data[coinName]["EUR"])); The JSON response for the coin "BTC" is: ...

Q.all failing to execute promises within array

Hey all, I'm currently facing an issue while attempting to migrate users - the promises are not being called. User = mongoose.model 'User' User.find({"hisId" : {$exists : true}}).exec (err, doc)-> if err console.error err ...

`Some Items Missing from Responsive Navigation Menu`

Hey there! I'm currently diving into the world of responsive design and I'm attempting to create a navigation bar that transforms into a menu when viewed on a mobile device or phone. Everything seems to be working fine, except that not all the na ...

What is the best way to display only a specific container from a page within an IFRAME?

Taking the example into consideration: Imagine a scenario where you have a webpage containing numerous DIVs. Now, the goal is to render a single DIV and its child DIVs within an IFrame. Upon rendering the following code, you'll notice a black box ag ...

JavaScript code that moves the active link to the top of the navigation when the window width is less than or equal to 800px

I'm working on a responsive navigation that is fixed at the top and switches from horizontal to vertical when the screen size is less than or equal to 800 pixels wide. However, I'm facing an issue with moving the active link to the top of the na ...

I need to input text into a specific element on the page, however there are four elements on the page that have identical properties

I am facing a dilemma where I need to input text into an element on the page. However, there are four elements with identical properties, making it challenging to find a unique locator. Does anyone have experience using Protractor for AngularJS web pages a ...

Navigating to a Website Based on the Selected Option in a Drop-Down Menu

I am currently working on a form that includes options for selecting a city and social networking site. The goal is to collect information about both the selected city and social network so that it can be stored for future reference. Upon submitting the fo ...

Jquery Droppable issue arising with dynamically added DIVs

I am facing a similar issue as described in this question and this one I am trying to implement drag-and-drop and resize functionality. It is working fine for static elements, but I encounter issues when adding dynamic divs. The resize property works prop ...

Issue with Datepicker not updating when triggered by event handler

Having an issue with my material-UI datepicker where the date is not updating correctly when I try to select a new one. The initial value set in useState works fine, but I want the datepicker to smoothly update when I choose a different date. You can see a ...

Tips for setting up a popup menu when clicking a button on a webpage

I am currently working on developing a popup menu with a greyed-out background that appears when the user clicks on a button in React. My code implementation is as follows: // The ifButtonClicked function is called when another button is clicked // Some ...

Tips for adding a new column to a website

My goal is to inject some custom HTML and CSS into YouTube in order to create a column on the right side that shifts all content towards the left. Essentially, I am trying to replicate the functionality of the Inspect Tool in Chrome. I am working on a Chr ...

Exploring jQuery's capabilities with Cross-Domain URLs

I created a basic index.php file with the following code: <!DOCTYPE html> <head> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript" src="js/httpGet.js"></script> <script type ...

JS and its dynamic color changes

A game has been developed using JavaScript and HTML. The game features a table with cells that are assigned IDs for toggling colors using an onClick function. The objective is simple: when a cell is clicked, all neighboring cells, including the clicked one ...