0
let rx = /(\/MxML\/[a-z0-9\[\]@/]*)/gi;
let s =
    'If (/MxML/trades[1]/value== 1 then /MxML/trades[type=2]/value must be /MxML/stream/pre/reference@href';

let m;
let res = [];

while ((m = rx.exec(s))) {
    res.push(m[1]);
}
console.log(res);
  1. Pattern should start with /MxML/
  2. Keep including acceptable characters [a-z0-9[]@/]
  3. A single = is ok. But == or === are not ok.

Desired output

[
    '/MxML/trades[1]/value',
    '/MxML/trades[type=2]/value',
    '/MxML/stream/pre/reference@href',
];

My current (incorrect) output

[
    '/MxML/trades[1]/value',
    '/MxML/trades[type',
    '/MxML/stream/pre/reference@href',
];

I can think of a lookaround, but can't figure out how to use it here. (?<!\=)\=(?!\=)

I've been going through several other examples on Stackoverflow, but they are about avoiding all character repetition and not about avoiding a particular character(=) repetition in conjunction with other valid characters.

0

1 Answer 1

1

Here is a regex which should be compatible in all browsers:

/(\/MxML\/(?:[a-z0-9\[\]@/]|=(?!=))*)/gi
  • /(\/MxML\/ - your regex
  • (?: - start non-capture group; we need this to implement an "or" operator and not mess up your use of m[1]
    • [a-z0-9\[\]@/] - your regex of whitelisted chars; notice the asterisk was removed
    • | - or
    • =(?!=) - allow for an equal sign not followed by an equal sign
  • )* - close non-capturing group and capture between zero and infinity instances of what's inside
  • )/gi - your regex

let rx = /(\/MxML\/(?:[a-z0-9\[\]@/]|=(?!=))*)/gi;
let s =
    'If (/MxML/trades[1]/value== 1 then /MxML/trades[type=2]/value must be /MxML/stream/pre/reference@href';

let m;
let res = [];

while ((m = rx.exec(s))) {
    res.push(m[1]);
}
console.log(res);

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

3 Comments

Thanks. Can you add a line of explanation please, including what is : for.
@KayaToast It's a non-capturing group. I didn't want to mess up your use of m[1]
@KayaToast I edited my answer to explain the regex a bit more

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.