0

I'm trying to use exec for a regular expression in node. I know the expression works via testing it with an extension in VSCode but when I run the node app it keeps returning null.`

str = '\r\nProgram Boot Directory: \\SIMPL\\app01\r\nSource File:  C:\\DRI\\DRI\\DRI Conf Room v2 20180419aj\r\nProgram File: DRI Conf Room v2 20180419aj.smw\r\n';

var regex = /\Program File:(.*?)\\/;
var matched = regex.exec(str);
console.log(matched);
3
  • What do you actually want to catch: DRI Conf Room v2 20180419aj.smw? Commented Jun 16, 2018 at 6:39
  • Yes. The str is actually much larger than this. Its to extract the name of the running code out of a manufacturer's product. Commented Jun 18, 2018 at 14:42
  • Sorry, I don't get that last comment. Does this mean your problem is not solved or something else? Commented Jun 18, 2018 at 23:53

3 Answers 3

1

I think you don't have to escape the \P at the beginning and the string ends with \r\n so you could match that instead of \\ which will match a backslash.

If you don't want the leading whitespace in the first capturing group you could add \s*to match zero or more whitespace characters: /Program File:\s*(.*?)\r\n/

For example:

str = '\r\nProgram Boot Directory: \\SIMPL\\app01\r\nSource File:  C:\\DRI\\DRI\\DRI Conf Room v2 20180419aj\r\nProgram File: DRI Conf Room v2 20180419aj.smw\r\n';

var regex = /Program File:(.*?)\r\n/;
var matched = regex.exec(str);
console.log(matched[0]);
console.log(matched[1]);

Demo

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

Comments

0

You need to use a RegExp constructor:

var str = '\r\nProgram \r\nProgram File: DRI 0180419aj.smw\r\n'
    .replace('[\\r,\\n]',''); // removes the new lines before we search

var pattern = 'Program File.+' // write your raw pattern
var re = new RegExp(pattern); // feed that into the RegExp constructor with flags if needed

var result = re.exec(str); // run your search
console.log(result)

Not really sure what your pattern should do, so I just put one there, that matches whatever starts with Program File. If you want all matches, not just the first, just change it to

var re = new RegExp(pattern,'g');

Hope that helps!

Comments

0

The regex syntax you use looks off. Try it like this:

const regex = /^Program File:\s*(.*?)$/gm;
const str = `
Program Boot Directory: \\\\SIMPL\\\\app01
Source File:  C:\\\\DRI\\\\DRI\\\\DRI Conf Room v2 20180419aj
Program File: DRI Conf Room v2 20180419aj.smw
`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

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.