0

How would I remove double quotes from all attributes of HTML elements using regular expressions in javascript? I tried the regex provided in this thread: Regex to remove quotes around attributes when possible

It works for most attributes, but if I have an anchor tag with an href like "page2.html" or "page2.html#someid" it doesn't remove the quotes.

Thanks

Community
  • 1
  • 1
mbhuiyan
  • 145
  • 2
  • 12
  • 1
    Why would you need to do this in the first place? Also if you want help you need to show the code and some sample data that it doesn't work on – charlietfl Jan 19 '16 at 02:37
  • 2
    [You can't parse (X)HTML with regex.](http://stackoverflow.com/a/1732454/1529630) – Oriol Jan 19 '16 at 02:38
  • I know it's bad practice, but I'm trying to match HTML generated by a program written in 2003 that doesn't include quotes around attributes. The database that my HTML would be uploaded to needs no-quotes to be able to parse the file correctly (which is dumb). Here's the regex that works for most attributes: `"([^"=.]+?)"` – mbhuiyan Jan 19 '16 at 02:46
  • What do you mean by "match HTML"? Why are you trying to remove quotes from your HTML, instead of adding quotes to the old HTML? An HTML clean-up library could do that for you. Or, you could parse the two HTMLs, ,and match the resulting DOMs. –  Jan 19 '16 at 04:31
  • I don't have control over the HTML I am trying to match (it's a web page generated by a different program). By match, I mean make my generated HTML look the same as the other HTML – mbhuiyan Jan 19 '16 at 21:08

1 Answers1

-1

You can use this:

var aString="\"abcde\""; //where this is yout variable 

and then

aString.replace(/"/g,"");

and you'll have all your quotes replaced

EDIT

The first parameter of the Replace function can be a Regular Expression

PekosoG
  • 246
  • 3
  • 9
  • How would I do this only for specific substrings? Like only replace the quotes after an href? – mbhuiyan Jan 19 '16 at 03:30
  • you can use Regular Expressions in the first parameter of the replace function. Sorry i didn't say that because you asked only for quotes – PekosoG Jan 19 '16 at 03:38
  • So your plan is to simply nuke all double quotes in the entire HTML document? What about double quotes in text? –  Jan 19 '16 at 04:33
  • Nope, my plan was to remove the quotes on a specific string, like retrieving the href attributes and then replace them... i never suggested to replace the entire HTML – PekosoG Jan 19 '16 at 18:33