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 9086347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T21:28:01+00:00 2026-06-16T21:28:01+00:00

I have an html file myfile.html , which includes a script with a line

  • 0

I have an html file myfile.html, which includes a script with a line like this:

var json = '[{"name":"Hydrogen","number":"1","symbol":"H","weight":"1.00794"},{"name":"Helium","number":2,"symbol":"He","weight":4.002602},{"name":"Lithium","number":3,"symbol":"Li","weight":6.941},{"name":"Beryllium","number":4,"symbol":"Be","weight":9.012182},{"name":"Boron","number":5,"symbol":"B","weight":10.811},{"name":"Carbon","number":6,"symbol":"C","weight":12.0107}]';

The string within single quotes that is assigned to variable json will actually vary. I would like to replace this string with the entire contents of another file myjson.json.

I tried with the code here:
Find and replace in a file in Ruby
and here:
search and replace with ruby regex
doing this:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub( %r{var json = '[^<]+';}, replace )

but its not working. I’m not sure if its the regex I’m doing incorrectly, or if its something more.

UPDATE

After reading the reply below, my first attempt was:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub!(%r{var json = '.+'}, replace)
puts changefile

This did the find correctly, but removed all of the var json = '' and replaced it with myjson.json – I want to keep var json = and only replace the contents between the two single quotes after. So then I tried:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub!(%r{var json = '.+'}, "var json = 'replace'")
puts changefile

But that just replaced it with var json = 'replace'

I want to use the original var json = to find the location, but I don’t want it to be removed.

So I did something I know is dumb and wrong, but it worked:

replace = File.read("myjson.json")
changefile = File.read("myfile.html")
changefile.sub!(%r{var json = '.+'}, "var json = 'thanksforthehelptinman'")
changefile.sub!(%r{thanksforthehelptinman}, replace)
puts changefile

Thanks for the help!

  • 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-16T21:28:03+00:00Added an answer on June 16, 2026 at 9:28 pm

    The regex isn’t right because [ and ] are reserved in regex. You need to escape them:

    %r{var json = '\[.+\]'}
    

    I can’t be more exact because I don’t know what’s in your JSON file, but that should get you into the ballpark.

    Also, unless you assign changefile.sub to something, the substitution will be thrown away. You can do one of these two things:

    changefile = changefile.sub(%r{var json = '\[.+\]'}, json)
    

    or mutate changefile:

    changefile.sub!(%r{var json = '\[.+\]'}, json)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have html-file. I have to replace all text between this: [%anytext%]. As I
I have a html file like following: <form action=/2811457/follow?gsid=3_5bce9b871484d3af90c89f37 method=post> <div> <a href=/2811457/follow?page=2&amp;gsid=3_5bce9b871484d3af90c89f37>next_page</a> &nbsp;<input
I have an HTML file and i would like to parse through it using
I have an HTML file which is launching an app if I open it
I have a base html file which I want all other pages to inherit
I have a string containing HTML but which also includes PHP tags ( <?php
I have a jquery script in my file that says this: <script type=text/javascript> $(document).ready(function(e){
I have created a HTML page which uses Calibri font. Now this font is
I have an html upload <input type=file id=myfile/> . When I click it, it
I found this ajax file upload script here http://www.phpletter.com/Demo/AjaxFileUpload-Demo/ Which is supposed to add

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.