I have strings similar in nature to 'foo = "bar"'. Right now when I am testing if these strings pass a certain regular expression, I am testing the quotes around ‘bar’ with ["|']?, making the full RegEx in that area something along the lines of ["|']?(\w+)["|']? (I am stripping out irrelevant expression parts for readability).
This means several things:
- The quotes around the word can be double quotes, single quotes, or nonexistent.
- Regardless of which they are, they are not “linked”. If a double quote is used on the left and no quote on the right this will still pass, etc.
My goal is to match the second quote based on the first. I would very much like to implement this entirely with RegEx, though I understand I am approaching the limits of what it can do (as far as the JavaScript implementation goes). I am quite familiar with lookaheads/behinds, but as I understand it these would only be relevant if the quotes were touching. I am hoping that I understand them incorrectly.
P.S.: I am only worried about modern browsers, so IE<10 support is a non-issue.
P.P.S: I mentioned that I would like to do this with RegEx. Please save yourself effort and do not give an alternative. A “you cannot do this” would suffice.
You could write:
\1will only match a string that’s identical to what the first capture-group ((["'])) captured.Note that I removed the pipe character
|from the character class["'], because it seemed to be an error: the pipe character does not have any special meaning inside a character class.["|']is only correct if you want to be able to match e.g.|bar|.