0

Given this text

unit: 100 street: 200 city: 300

How do I write a regex that will give this output as different groups in JS. I also need to mention that the text may not contain street or city. It could just be unit: 100.

group[1] - 100
group[2] - 200
group[3] - 300

Where I'm at so far - unit:\s(.*?)\s(?:.*|$)

Not sure how to proceed further and get all 3 groups in one regex!

4
  • /:\s(\d{3})/? Commented Oct 20, 2022 at 0:00
  • Do you have a JSON string? Commented Oct 20, 2022 at 0:00
  • It need not be numbers, could be any text. Commented Oct 20, 2022 at 0:01
  • @TimBiegeleisen Not a json string. Its from a log file with a similar format. Commented Oct 20, 2022 at 0:05

4 Answers 4

1

You may do this using split as well:

const s = 'unit: 100 street: 200 city: 300';

var arr = s.split(/\s*\w+:\s*/).filter(Boolean);

console.log( arr );

//=> ["100", "200", "300"]

Here \s*\w+:\s* matches 0 or more spaces, followed by 1+ word characters then : and 0 or more whitespaces.

Note the filter(Boolean) is just used to remove empty elements from resulting array.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a single capture group:

[^\s:]+:[^\S\n]*(\S+)

Regex demo

const regex = /[^\s:]+:[^\S\n]*(\S+)/g;
[
  `unit: 100 street: 200 city: 300`,
  `unit: 100`
].forEach(s =>
  console.log(
    Array.from(s.matchAll(regex), m => m[1])
  )
);

Or you can use optional nested groups, but the pattern will be longer if you want to use more groups:

[^\s:]+:[^\S\n]*(\S+)(?:[^\S\n]+[^\s:]+:[^\S\n]*(\S+)(?:[^\S\n]+[^\s:]+:[^\S\n]*(\S+))?)?

Regex demo

Comments

0

We can use match() here:

var input = "unit: 100 street: 200 city: 300";
var matches = input.match(/(?<=: ).*?(?=\s*\w+:|$)/g);
console.log(matches);

Comments

0
const rgx = /\D*(\d)(\d)+/g

Regex101

Segment Description
\D*
Zero or more non-digits
(\d)
First capture group of a digit
(\d)+
Second capture group of one or more digits
g
global flag

.replace() with

"group[$1] - $1$2$2\n"
Segment Description
group[$1]
Replace with literal: group[, then the first capture group of (\d), and then a literal: ]
 - 
Then a space, a literal hyphen -, and a space
$1$2$2\n
Next, the first capture group again: (\d), then the second capture group: (\d)+ twice, and finally a newline

Example A

const str = `unit: 100 street: 200 city: 300`;
const rgx = /\D*(\d)(\d)+/g;

const res = str.replace(rgx, "group[$1] - $1$2$2\n");
console.log(res);

Note: The following comment was not considered in Example A:

It need not be numbers, could be any text."

The question should have this criteria added and an appropriate example:

'unit: 100 street: Main city: Springfield'

Because of this part: group[?], more than one method is needed. See Example B for a solution.

Example B

const str = 'unit: 100 street: Main city: Springfield';
const rgx = /\b(\w+:) ([\w]+)/g;

const res = str.replace(rgx, "$2")
            .split(' ')
            .map((s, i) => 
              "group["+(i + 1)+"] - "+s).join('\n');

console.log(res);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.