Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:01:45+00:00 2026-06-14T08:01:45+00:00

I have an html string and I want to replace any instance of an

  • 0

I have an html string and I want to replace any instance of an html attribute being set with single quotes with double quotes.

So for example, I want to replace

<script src='foo.js'></script>

with

<script src="foo.js"></script>

However, I want to do this without affecting any single quotes that might be in javascript statements or in text within the html.

Eg

<script> var foo = '67'; </script> 

should be unaffected and

<div id='foo'> 'hi' </div>

should become

<div id="foo"> 'hi' </div>

Is there any easy way to do this?

For a given element selecting it with jquery and then reading its outerHTML does this but I want to do it to an entire page of html all at once.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:01:46+00:00Added an answer on June 14, 2026 at 8:01 am

    Answering my own question as I think the easiest solution to this is what is shown in this fiddle and does not require jquery or regexps:

    http://jsfiddle.net/QdUR5/1/

    <html id="foo"></html>​
    var htmlString = 
      '<head>' +
      '<script type="text/javascript" src=\'main.js\'></scr' + 'ipt>' +
      '</head>' +
      '<body onload=\'onLoad()\'>' +
      '</body>' ;
    
    document.getElementById('foo').innerHTML = htmlString;
    console.log(document.getElementById('foo').outerHTML);
    

    ​

    Basically you just need to set the inner html of an html element to the html string without the html tags and then output the html elements outer html.

    I think that is a bit simpler than using a regexp although that is an awesome regexp Mike 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML string and want to replace all links to just a
I have a string (partly HTML) where I want to replace the string :-)
I have an HTML document as a string I want to search for a
I have some html extracted to a string var, and want to then use
I have a HTML document with the a table which I want to replace,
if i have a string like <p>this is some content</p><script>alert('hello');</script> i want to get
I have this string with HTML inside: <span title=whatever>something I want to preserve</span>... I'm
If you have a string containing HTML entities and want to unescape it, this
I want to strip html string for Span tags. I have a html string
I have a string that looks like - /celebrity.html#portfolios/jennifer-aniston-2006#img10 I want to remove #img10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.