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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:11:54+00:00 2026-05-15T15:11:54+00:00

Would like to use jQuery to: select any punctuation marks (meaning . , ;

  • 0

Would like to use jQuery to:

  1. select any punctuation marks (meaning . , ; : ' " “ ” ‘ ’ etc.) that occur directly before and after any links and then
  2. wrap the punctuation marks in spans.

Hence this:

<div id="mydiv">
  Lorem ipsum
  <a href="#">dolor sit amet</a>,
  consectetur
  &ldquo;<a href="#">adipisicing elit</a>&rdquo;.
</div>

to become this:

<div id="mydiv">
  Lorem ipsum
  <a href="#">dolor sit amet</a><span>,</span>
  consectetur
  <span>&ldquo;</span><a href="#">adipisicing elit</a><span>&rdquo;.</span>
</div>

(BTW, &ldquo and &rdquo; are quotation marks.)

The content in the div is not fixed. There may also be instances where there’s more than one punctuation mark adjacent to a link (as in the above, &rdquo;. to become <span>&rdquo;.</span>)

Would really appreciate any help!!


Sorta similar (but not quite) to this question with regards to selecting text nodes? Using jQuery, how to modify text outside of tags?

  • 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-15T15:11:55+00:00Added an answer on May 15, 2026 at 3:11 pm

    This seems to work:

    Try it out: http://jsfiddle.net/GutKg/1/

    var $div = $('#mydiv');
    
    var html = $div.html();
    
    html = html.replace(/([^\s\w]+)<a/g, '<span>$1</span><a')
               .replace(/<\/a>([^\s\w]+)/g, '</a><span>$1</span>');
    
    $div.html( html );​
    

    EDIT: Updated to exclude alphanumeric characters adjacent to the tags.

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

Sidebar

Related Questions

I would like to know how can I use jQuery to select multiple items
I would like use Jquery to submit a form automatically once I select a
I would like to use jquery to update my attribute value, because I want
We would like to use jquery mobile for all aspects of our site, but
I know this is very basic. I would like to use jQuery to change
I would like to .get a page and then use jquery to find elements
I would like to use this script https://github.com/jeromeetienne/jquery-qrcode (or is there even a better
I'm trying to avoid jQuery and would like to use a native solution. How
I would like to know if I can use the following JQuery function on
I'm using the jQuery Expander plug-in. I would like it to use an effect

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.