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

  • Home
  • SEARCH
  • 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 6181105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:57:44+00:00 2026-05-24T00:57:44+00:00

<script type=text/javascript> var haystackText = document.getElementById(navigation).innerHTML; var matchText = ‘<a href=http://mydomain.co/?feed=rss>Subscribe to RSS</a>’; var

  • 0
<script type="text/javascript">
   var haystackText = document.getElementById("navigation").innerHTML;

   var matchText = '<a href="http://mydomain.co/?feed=rss">Subscribe to RSS</a>';

   var replacementText = '<ul><li>Some Other Thing Here</li></ul>';

   var replaced = haystackText.replace(matchText, replacementText);

   document.getElementById("navigation").innerHTML = replaced;
</script>

I’m attempting to try and replace a string of HTML code to be something else. I cannot edit the code directly, so I’m using Javascript to alter the code.

If I use the above method Matching Text on a regular string, such as just ‘Subscribe to RSS’, I can replace it fine. However, once I try to replace an HTML string, the code ‘fails’.

Also, what if the HTML I wish to replace contains line breaks? How would I search for that?

<ul><li>\n</li></ul>

??

What should I be using or doing instead of this? Or am I just missing a small step? I did search around here, but maybe my keywords for the search weren’t optimal to find a result that fit my situation…

Edit: Gonna mention, I’m writing this script in the footer of my page, well after the text I wish to replace, so it’s not an issue of the script being written before what I want to overwrite to appear. 🙂

  • 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-05-24T00:57:45+00:00Added an answer on May 24, 2026 at 12:57 am

    Currently you are using String.replace(substring, replacement) that will search for an exact match of the substring and replace it with the replacement e.g.

    "Hello world".replace("world", "Kojichan") => "Hello Kojichan"
    

    The problem with exact matches is that it doesn’t allow anything else but exact matches.

    To solve the problem, you’ll have to start to use regular expressions. When using regular expression you have to be aware of

    • special characters such as ?, /, and \ that need to escaped \?, \/, \\
    • multiline mode /regexp/m
    • global matching if you want to replace more than one instance of the expression /regexp/g
    • closures for allowing multiple instances of white space \s+ for [1..n] white-space characters and \s* for [0..n] white-space characters.

    To use regular expression instead of substring matching you just need to change String.replace("substring", "replacement") to String.replace(/regexp/, "replacement") e.g.

    "Hello world".replace(/world/, "Kojichan") => "Hello Kojichan"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: <script type=text/javascript> function js() { var getJs = document.getElementById(jogo); if
Below is the code for my YWA wrapper var astr_ywascript = (document.createElement(script).type = text/javascript).src
The code is like this: var script = document.createElement('script'); //script.type = 'text/javascript'; // do
<html> <head></head> <body> <script type = text/javascript> var x = 5; var y =
Look this code: <script type = text/javascript> function mouseClick (container) { container.appendChild (document.createTextNode (Can
I am using the following technique to load up Javascript dynamically: var script =
I was wondering if anyone here had some experience writing this type of script
What should I type on the Mac OS X terminal to run a script
I want to make sure people can't type the name of a PHP script
I have opened a new window with JavaScript: var newwin = window.open('','preview','width=600,height=500'); Now I

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.