简体   繁体   English

分组正则表达式BackReferences

[英]Grouping Regular expression BackReferences

I have the following RegEx 我有以下正则表达式

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

The reason for this is I am trying to replace Ids from the browsers DOM using JavaScript. 原因是我正在尝试使用JavaScript从浏览器DOM中替换ID。 IE, however strips quotes from element atributes as it appears not to require them in the DOM IE,但是从元素属性中删除引号,因为它似乎不需要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? 我的问题是,每个替代语句的反向引用都位于单独的组中($ 1和$ 2),因为匹配项只有一个,或者另一个匹配项可以同时将两个反向引用作为单个反向引用返回吗?

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 我只希望将两个反向引用都添加到$ 1中

what about: 关于什么:

id="?(.*?)"?

(possibly that . should be [^"] - I didn't test it) (可能是。应该是[^“]-我没有测试过)

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

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. 但是,要仅检索属性值,如果用引号引起来,则必须在开头/结尾处去除"'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM