Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
RegEx to match whole line after nth occurrence of a string

Long time lurker, first time poster! Thanks to anyone in advance that can help.

I'm using Node.JS and Electron to make a little desktop app that manages the values in a game file (which is from a custom file type, basically a glorified INI file). With the idea of letting people customise the file in a nice UI as opposed to combing through hundreds of lines of code. Below is a sample from the file:

setting $Low
    prop $Config A_STRING_HERE "5, 10, 15, 20"

setting $Medium
    prop $Config A_STRING_HERE "10, 20, 40, 80"

setting $High
    prop $Config A_STRING_HERE "20, 40, 80, 160"

setting $VeryHigh
    prop $Config A_STRING_HERE "40, 80, 160, 320"

How would I go about matching, for example, the "setting $High" (3rd) occurrence containing "A_STRING_HERE" until the end of the line. I'm hoping to match just:
A_STRING_HERE "20, 40, 80, 160".

The values are dynamic and will change a lot, so matching the line with the values isn't necessarily an option.

  • \A_STRING_HERE.+\g will match all the occurrences to the end of the line.
  • \A_STRING_HERE.+\ will match the first occurrence to the end of the line.
  • \A_STRING_HERE.+\?????? will match the 3rd occurrence to end of the line?

Thanks again and all the best!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can probably try this regex:

(?<=setting \$High\n\s*?prop \$Config )A_STRING_HERE.*

Explantion

  • (?<=) - Represents positive lookbehind.
  • setting \$High\n\s*?prop \$Config - The text that separates this match with other matches. You can probably omit setting part to make it more concise.
    • \n - Represents new line.
    • \s*? - Lazily matches 0 or more white spaces. You can adjust it according to your need.
  • .* - Matches everything except \n. You can be specific here as well if you want to include numbers only.
  • \g - Please note only global flag is used.

You can find the implementation of the above regex in here.

Image Explanation: Image

const regex = /(?<=setting \$High\n\s*?prop \$Config )A_STRING_HERE.*/g;

// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<=setting \\$High\\n\\s*?prop \\$Config )A_STRING_HERE.*', 'g')

const str = `setting \$Low
    prop \$Config A_STRING_HERE "5, 10, 15, 20"
setting \$Medium
    prop \$Config A_STRING_HERE "10, 20, 40, 80"
setting \$High
    prop \$Config A_STRING_HERE "20, 40, 80, 160"
setting \$VeryHigh
    prop \$Config A_STRING_HERE "40, 80, 160, 320"`;
console.log(str.match(regex)[0]);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!