0

I am trying to extract data from a string using RegEx , but i am getting a NULL value as result.

here is my current code

var re = /(\[cid=(?:[0-9]*)(?:(?:,\[[^]]*\][^]]*)?|(?:,[^]]*))\])/;
var str = '[cid=5555,[CONSTIMG]5555.jpg]The Sample text is awesome';
var m;

while ((m = re.exec(str)) != null) {
    if (m.index === re.lastIndex) {
        re.lastIndex++;
    }

}

console.log(m[0]);

The value that i am getting back is NULL.

If anyone can point me in the right direction, that would be very helpful. Thanks in advance.

4
  • 4
    What are you trying to find? I can't attempt to help find a working regex if I don't know what you are looking for. Also, have you tried testing on a site like regexpal.com ? Commented Feb 24, 2015 at 22:59
  • It is because your expression does not match your input. Commented Feb 24, 2015 at 23:00
  • @David it's showing as valid here, postimg.org/image/egmgh1n2f Commented Feb 24, 2015 at 23:04
  • Try this (\[cid=\d*,(?:\[(?:.[^\]]*\]){1,2})?) Commented Feb 24, 2015 at 23:22

1 Answer 1

2

Your expression matches PCRE regular expression syntax but not JavaScript because JavaScript requires that square brackets inside a character class be escaped with \. This is what you want:

(\[cid=(?:\d*)(?:(?:,\[[^\]]*\][^\]]*)?|(?:,[^\]]*))\])

Explained: https://regex101.com/r/pN4vP4/2

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

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.