简体   繁体   中英

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?

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]); } 

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); 

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.

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())); 

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM