views:

63

answers:

1

Hi,

Does javascript regular expressions support backreferences inside character class?

I want to do something like this:

htmlString.replace(/attribute_name=(['"])[^\1]*\1/,'')

But that does not work. This does:

htmlString.replace(/attribute_name=(['"])[^'"]*\1/,'')

Unfortunatelly my attribute_name can contain apostrophes or quotes, so I need to exlude the actual quoting character from the inside of the attribute, but leave the other one. I can't be sure which one is used. I can safely assume that quotes are in form of entity, but still there can be apostrophes inside:

<div attribute_name="John's car" class="someClass"></div>
<div attribute_name='some &quot;quoted text&quot;' class="someClass"></div>

I am not able to predict which of " or ' will be used around the attribute.

How to get rid of the attribute and leave the class attribute alone (not cut too much)?

context: I am getting the html by $('templateContainer').innerHTML . I have to modify that html before inserting it into the page again. I have to cut some non-standard attibutes and all the ID attributes.

+1  A: 

You'd be a LOT better off using DOM or some other actual model designed for hierarchical content. That said, if you must use regex, the simplest way would probably be to just use a | (OR) instead.

htmlString.replace(/attribute_name=('[^']*'|"[^"]*")/,'')
Amber
Yeah, simplest solutions are hardest to find.I know it would be better to use DOM, but i need to do this before I put it back in DOM. It is a result of previous mistake in project and I have to little time to redesign it. Thank you.
SWilk