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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:32:36+00:00 2026-05-25T23:32:36+00:00

For the sake of simplicity & learning something new, please don’t suggest using two

  • 0

For the sake of simplicity & learning something new, please don’t suggest using two separate replace functions. I know that’s an option but I would rather also know how to do this (or if it’s not possible).

'<test></test>'.replace(/<|>/g,'$&'.charCodeAt(0))

This is what I’ve got so far. This sample code is, as you can tell, for another piece of code to escape HTML entities while still using innerHTML (because I do intend to include a few HTML entities such as small images, so again please don’t suggest textContent).

Since I’m trying to replace both < and >, the problem is converting each individual one to their respective character codes. Since regular expressions allow for this “OR” condition as well as backreferences to each one, I’m hoping there’s a way to get the reference of each individual character as they’re replaced. $& will return <><> (because they’re replaced in that order), but I don’t know how to get them as they’re replaced and take their character codes for the HTML entities. The problem is, I don’t know what to use in this case if anything.

If that explanation wasn’t clear enough, I want it to be something like this (and this is obviously not going to work, it’ll best convey what I mean):

Assuming x is the index of the character being replaced,

'<test></test>'.replace(/<|>/g,'$&'.charCodeAt(x))

Hopefully that makes more sense. So, is this actually possible in some way?

  • 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-25T23:32:37+00:00Added an answer on May 25, 2026 at 11:32 pm
    '<test></test>'.replace(/[<>]/g,function(a) {return '&#'+a.charCodeAt(0)+';';});
    

    I’ve put the characters in a square-bracket-thing (don’t know it’s proper name). That way you can add whatever characters you want.

    The above will return:

    &#60;test&#62;&#60;/test&#62;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the sake of simplicity, let's say my database has two tables, videos and
For the sake of simplicity, assume I have two entities configured as follows: Topic.cfc
I am using EF 4.1 Code First and for the sake of simplicity, let's
For the sake of simplicity: There is a permission based system in place with
I am creating a custom WPF control that let's say for simplicity sake has
I have a few procedures, for simplicity sake, look like the following: public string
I have an NSMutableArray that is populated with objects of strings. For simplicity sake
Suppose you have a standard org hierarchy table in Oracle. For simplicity sake, assume
I'm working with three tables, and for simplicity's sake let's call them table A,
I have a dilemma. Let's assume(for simplicity's sake) I have four tables, with different

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.