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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:45:16+00:00 2026-06-13T20:45:16+00:00

Suppose I have a string like this: <code>Blah blah Blah enter code here</code> <code

  • 0

Suppose I have a string like this:

<code>Blah blah Blah
enter code here</code>
<code class="lol">enter code here
fghfgh</code>

I want to use javascript to replace all occurences between the <code> tags with a callback function that html encodes it.

This is what I have currently:

function code_parsing(data){
    //Dont escape & because we need that... in case we deliberately write them in
    var escape_html = function(data, p1, p2, p3, p4) {
        return p1.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#039;");
    };

    data = data.replace(/<code[^>]*>([\s\S]*?)<\/code>/gm, escape_html);
        // \[start\](.*?)\[end\]
        return data;        
    };

This function is unfortunately removing "<code>" tags and replacing them with just the content. I would like to keep the <code> tags with any number of attributes. If I just hardcode the <code> tag back into it, I will lose the attributes.

I know regex isn’t the best tool, but there won’t be any nested elements in it.

  • 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-13T20:45:17+00:00Added an answer on June 13, 2026 at 8:45 pm

    You shouldn’t use regular expressions to parse HTML.

    That said, you need to capture the content you want to preserve using a parenthetical group and have your replacer append that to the bit you manipulate.

    data.replace(/(<code[^>]*>)([\s\S]*?)(<\/code>)/g,
                 function (_, startTag, body, endTag) {
                   return startTag + escapeHtml(body) + endTag;
                 })
    

    To understand why you shouldn’t use regular expressions to parse HTML, consider what this does to

    <code title="Shows how to tell whether x > y">if (x &gt; y) { ... }</code>
    
    <code lang="js">node.style.color = "<code lang="css">#ff0000</code>"</code>
    
    <code>foo</CODE >
    
    <textarea><code>My HTML code goes here</code></textarea>
    
    <code>foo  <!-- commented out </code> --></code>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function like this (I use akka 2.0.2): def foo(message: String):
Suppose I have some javascript code like this: function Person(){} var pro=Person.prototype; pro[setName]=function(){} pro[setAge]=function(){}
I have a data structure that represents C# code like this: class Namespace: string
Suppose I have some code like this: public string SomeMethod(int Parameter) { string TheString
Suppose I have a string like this: one two three four five six seven
Supposed I have two classes like this: public class Parent { public string Id
In perl suppose I have a string like 'hello\tworld\n' , and what I want
Suppose I have a MEF composition like this: public class Composition { [ImportMany(AllowRecomposition =
Suppose I have a class like this: package com.spotonsystems.bulkadmin.cognosSDK.util.Logging; public class RecordLogging implements LittleLogging{
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {

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.