marshall94pl
marshall94pl

Reputation: 41

Extracting specific data from a string

I have a large text from which I want to extract data according to a specific scheme.

Scheme:

(something) something (something|something)

for example

(Point A) 147 (-10|22) or (City) 479 (147|-20)

There are many such phrases in my text and I would like to write each one to array.

Upvotes: 1

Views: 79

Answers (1)

Guerric P
Guerric P

Reputation: 31805

This regexp should do the trick for you:

const input = 'Some other text (City) 479 (147|-20) Some other text';

const result = /\(\s*([^)]*)\s*\)\s*([^(]*)\s*\(\s*([^|]*)\s*\|\s*([^)]*)\s*\)/g.exec(input);

console.log(result);

Upvotes: 1

Related Questions