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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:11:14+00:00 2026-06-14T19:11:14+00:00

I have HTML like this example: <div class=code> function something(a, b) { return 0;

  • 0

I have HTML like this example:

<div class="code">
  function something(a, b) 
  {
     return 0;
  }
</div>

I want to surround all parentheses, brackets and braces with a span of class brackets. Like this:

<div class="code">
  function something<span class="brackets">(a, b<span class="brackets">)</span> 
  <span class="brackets">{</span>
     return 0;
  <span class="brackets">}</span>
</div>

My JavaScript/jQuery so far:

$('.code').each(function() {

  $(this).html(
    $(this).html().replace("???", '<span class="brackets">$1</span>')
  );

});

I really don’t know what to put in place of ???. Anything I can find on the internet is people wanting to match between brackets not the actual brackets themselves.

Just to be clear I want to replace all instances of (, ), {, }, [ and ].

Any help would be hugely appreciated.

  • 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-14T19:11:15+00:00Added an answer on June 14, 2026 at 7:11 pm

    You can just stuff them all into a character class (which matches any single character inside the class). The character class itself is denoted by square brackets, so you will have to escape the ones inside (that is only partially true, but it’s definitely good practice):

    /([(){}\[\]])/g
    

    The outer round parentheses are there so that the captured value actually ends up as $1. The g is there so that all occurrences are replaced.

    Note that this is not a string! Do not surround this with ". Simply supply it as the first argument to replace as it is:

    ...html.replace(/([(){}\[\]])/g, '<span class="brackets">$1</span>')
    

    By the way, you should probably iterate over all text nodes in the DOM and apply the replacement only to those. Otherwise you might add in that tag into attribute values or comments (or worse, embedded JavaScript or CSS).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have html code like this <tr class=odd> <td><b>Reason for Termination:</b></td> <td>this has bunch
I have HTML code like this: <asp:LinkButton ID=AddButton runat=server OnClick=AddPatientBtn_Click> <span class=Normal>Add</span> </asp:LinkButton> I
I have html structure like this: <div class='inside'> <div>429185</div> <div>492128</div> </div> Sometimes there is
I have a template like this: <script id=notesTemplate type=text/html> <li class=Note> <div class=NoteDate data-bind=style:
I have this code in my html: <form> <div style=width:530px; height:220px; overflow-y:scroll;> <div class=people>
I have HTML like this: <div> <a href=>sometext<img src=images/001.jpg /></a> <a href=>sometext<img src=images/002.jpg /></a>
I have HTML like this: <a href=# class=modalInput glow rel=#newsletter >Newsletter!</a> This opens a
So I have Html like this http://trac.edgewall.org/wiki/RecentChanges (I want to create some Flash Track
I have html markup like this <button id=button1 onclick=alert='yup'></button> and I want to make
I have the following Javascript/jQuery function: function addEventHandler(){ $(div).mouseenter(function() { $(this).html(Over); }).mouseleave(function() { $(this).html(Out);

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.