Ad

Regex String Split With Multiple Conditions

- 1 answer

(Javascript older version of Node.js)

Edit I have to clear up what I want.

I have multiple lines like these

var name1;

CONST    name2

   Let  nam;

leT     nam

  VAr n1  ;

My desired outputs are

name1

name2

nam

nam

n1

So I am trying codes like this.

/[^;]*/

But I need help because I have multiple patterns. Some may have semicolons/space/null after the variable names, I need to omit those.

Ad

Answer

use /(?<=(var\s+)|(let\s+)|(const\s+))(\w+)/gim

let data = `
var name1;

CONST    name2

   Let  nam;

leT     nam

  VAr n1  ;
`;
data = data.match(/(?<=(var\s+)|(let\s+)|(const\s+))(\w+)/gim);
console.log(data); // [ 'name1', 'name2', 'nam', 'nam', 'n1' ]

Ad
source: stackoverflow.com
Ad