Grouping Regular Expression BackReferences

I have the following RegEx

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

      

The reason for this is because I am trying to replace IDs from the DOM of browsers with JavaScript. IE however rolls strings from element attributes since it doesn't require them in the DOM

The problem is that the backrefererences from each alternate operator are in separate groups ($ 1 and $ 2), since the match is only one OR the other can return as a backreference as one backlink?

EDIT:

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

      

will match as follows

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

      

I just want both backlinks to be appended to $ 1

0


source to share


3 answers


id="?([\w-]*)"?

      



0


source


What about:

id="?(.*?)"?

      



(maybe it should be [^ "] - I haven't tested it)

0


source


Using an identifier

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

      

This will match

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

      

But to get just the value of an attribute, you have to strip "

or '

start / end if it is surrounded by quotes.

0


source







All Articles