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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:34:05+00:00 2026-06-08T07:34:05+00:00

This would best be explained with examples. Here is a line before I do

  • 0

This would best be explained with examples.

Here is a line before I do anything:
Monohydrogen_Phosphate HPO%4^2-

Here is what I’ve done so far: Monohydrogen Phosphate | HPO%4^2-

Here is what it should be when finished: Monohydrogen Phosphate | HPO42-

The % will put the first number (if any) and + or – signs (if any) in a <sub> tag, and the ^ will put the first number and +/- in a <sup> tag.

I am using Javascript’s RegEx replace, but I don’t mind switching to PHP.

  • 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-08T07:34:06+00:00Added an answer on June 8, 2026 at 7:34 am

    I would use

    str.replace(/([^\|])\s+([a-z]+)(%(\d+[+-]?))?(\^(\d+[+-]?))?/gi,'$1 | $2<sub>$4</sub><sup>$6</sup>');
    

    Here’s an example:

    <script type="text/javascript">
    
        var str = 'Monohydrogen_Phosphate HPO%4^2-';
        var str1 = str.replace(/([^\|])\s+([a-z]+)(%(\d+[+-]?))?(\^(\d+[+-]?))?/gi,'$1 | $2<sub>$4</sub><sup>$6</sup>');
        console.log('str:\t' + str + '\nstr1:\t' + str1);
    
    </script>
    

    which will output:

    str: Monohydrogen_Phosphate HPO%4^2-
    str1: Monohydrogen_Phosphate | HPO<sub>4</sub><sup>2-</sup>

    which, in HTML will be parsed like…

    Monohydrogen_Phosphate | HPO42-


    EDIT:

    I was thinking about compound(?) elements such as Li2CO3 so I came up with a longer but better solution.

    function formatStr(str) {
    
        return str.replace(/([a-z]+)(%(\d+[+-]?))?(\^(\d+[+-]?))?/gi,function() {
        // this part allows stuck elements to be parsed right
        // such as Li%2CO%3 or H%2SO%4
            if (!arguments[3] && !arguments[5]) return arguments[0];
            var _str = arguments[1];
            _str += arguments[3] ? '<sub>' + arguments[3] + '</sub>' : '';
            _str += arguments[5] ? '<sup>' + arguments[5] + '</sup>' : '';
            return _str;
        }).replace(/(.+)\s+([a-zA-Z]+<su[bp]>.+<\/su[bp]>)$/,'$1 | $2');
        // and this part adds ' | ' to the beginning of the changed element
        // if there's any content before it. otherwise it's left as is
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Wondering what the best / good way of doing this would be in jQuery.
I've been wondering what the best way to do this would be. I've got
How best would I exclude results from #other_section, given a structure like this? <form>
If anyone has any experience in this framework: https://github.com/eczarny/xmlrpc/ , it would be best,
In a single paged application, would this be the best way to bind and
What would be the best way to something like this where a timestamp is
What would be the best practice for sharing localization object (in this case ResourceBundle,
What would be the best algorithm to solve this problem? I spent a couple
Which is the best practice in this situation? I would like an un-initialized array
Beginner here, I have a simple question. In Android what would be the best

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.