views:

530

answers:

3

I have the following RegEx

id=(.*?) | id="(.*?)"

The reason for this is I am trying to replace Ids from the browsers DOM using JavaScript. IE, however strips quotes from element atributes as it appears not to require them in the DOM

The problem I have is that the backrefererences from each alternate statement are in separate groups ($1 and $2) as the match is only one OR the other can I return both the backreference as a single backreference?

EDIT:

<div id="test1" /><div id=test2 />

will match as follows

    match         |  $1   |   $2
--------------------------------
    id="test1"    | test1 |
    id=test2      |       |  test2

I just want both backreferences to be added to $1

A: 

what about:

id="?(.*?)"?

(possibly that . should be [^"] - I didn't test it)

annakata
This will only match id=" and misses the rest
Sheff
should have been "?([^"]*)"? then, c'est la vie
annakata
No, the problem is that everything after the equals sign is optional.
Alan Moore
But yes, you should have used [^"]* as well.
Alan Moore
um, no /id="?([^"]*)"?/ works exactly as expected - the ? being equivalent to {0,1} when used as a quantifier
annakata
It works becasue the dot was replaced with the negated character class, AND because the reluctant quantifier was replaced with a greedy one. Both changes are necessary.
Alan Moore
so as per my comment an hour before yours? I did say it wasn't tested
annakata
A: 
id="?([\w-]*)"?
Diadistis
Won't match if an ID contains a dash...
J-P
Nope, it also needs to match colons, periods and the first character of an ID has to be a letter.
J-P
*sigh* can we just assume the OP is going to manage that himself given the question was about grouping, not about valid ID strings?
annakata
A: 

I’d use

id\s*=\s*("[^"]*"|'[^']*'|[^\s>]*)

This would match

id="foo"
id='bar'
id=baz

But to retrieve just the attribute value, you have to strip the " or ' at the begin/end if it’s surrounded by quotes.

Gumbo