Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex returns eslint and ts errors

There is a text file which has key-value pairs as content. The value can optionaly be quoted.

test="value"

Now I need to get the value of a specific keyof, which I do with this regex:

/test=(")?(.*)(?(1)\1|)/m

This is working: https://regex101.com/r/DBAqOx/1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

But in the editor

const regex = new RegExp('^' + key + '=(")?(.*)(?(1)\1|)$', 'm')

I do get two errors for this codeline:

Parsing error: Octal escape sequences are not allowed. Use the syntax '\x01'. eslint
Octal escape sequences are not allowed. Use the syntax '\x01'. ts(1487)

>Solution :

A minor typo, but worth explaining in an answer. Because you are using a string literal with the RegExp() constructor, you would need to double escape the backreference as \\1:

const regex = new RegExp('^' + key + '=(")?(.*)(?(1)\\1|)$', 'm')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading