Ehsan
Ehsan

Reputation: 12951

How do I get the words out of parenthesis using regular expression

I want to get words out of parenthesis using a regular expression.

This is my code :

var patt = /(?!.+\))\w+/g;
var str = '( hello ) ehsan (how are) you' ;
console.log( patt.exec(str) ) ;
console.log( patt.exec(str) ) ;

Actual result

you , null

Expected result

ehsan , you

There is a way through negative lookahead?

Upvotes: 2

Views: 84

Answers (5)

revo
revo

Reputation: 48711

You have two choices to go with. The first would be matching everything inside parentheses then any remaining words. Afterwards you can filter them easily:

var str = '( hello ) ehsan iran (how are) you';
console.log(
  str.match(/\([^()]*\)|\w+/g).filter(x => x[0] !== '(')
)

The second approach is tricking a negative lookahead:

var str = '( hello ) ehsan iran (how are) you';
console.log(
  str.match(/\w+(?![^()]*\))/g)
)

The first approach is reliable. The second needs all parentheses to be paired and correctly closed.

Upvotes: 1

hygull
hygull

Reputation: 8740

The below statements give the ouptput that you expect.

var patt = /(?!.+\))\w+/g;
var str = '( hello ) ehsan (how are) you' ;

var arr = str.split(/\([\w\s]*\)/);   // [ '', ' ehsan ', ' you' ]
var arr = arr.filter((s) => s!== ""); // [ ' ehsan ', ' you' ]

var s = ("" + arr).trim(); 
console.log(s); // ehsan , you

Upvotes: 0

The fourth bird
The fourth bird

Reputation: 163217

Your regex uses a negative lookahead (?!.+\) to assert what is on the right is not a closing parenthesis. That has matches from the last occurence of the closing parenthesis on because after that, there are no more ). Then you match 1+ word characters which will match you.

Instead of using a negative lookahead, you coud to use a capturing group:

\([^)]+\)\s*(\w+)

Regex demo

const regex = /\([^)]+\)\s*(\w+)/g;
const str = `( hello ) ehsan (how are) you`;
let m;

while ((m = regex.exec(str)) !== null) {
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }
  console.log(m[1]);
}

If the engine support lookbehind with accepts infinite length quantifiers, you might also use a positive lookbehind:

(?<=\([^()]+\)) (\w+)

const regex = /(?<=\([^()]+\))\s*(\w+)/g;
const str = `( hello ) ehsan (how are) you`;

while ((m = regex.exec(str)) !== null) {
  if (m.index === regex.lastIndex) {
    regex.lastIndex++;
  }
  console.log(m[1]);
}

Upvotes: 1

Code Maniac
Code Maniac

Reputation: 37755

You can do it like this

First remove all the character between () and than split it with space.

var str = '( hello ) ehsan (how are) you' ;

let op = str.replace(/\(.*?\)/g, '').trim().split(/\s+/)
console.log(op);

Upvotes: 1

str
str

Reputation: 44969

An easy way to solve it is split:

const input = '( hello ) ehsan (how are) you';
const output = input.split(/\(.+?\)/);

console.log(output);
console.log(output.filter(v => v));
console.log(output.filter(v => v).map(v => v.trim()));

Upvotes: 0

Related Questions