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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:08:04+00:00 2026-06-06T20:08:04+00:00

I am using JavaScript’s replace() function to replace a regular expression matching A-Z, a-z,

  • 0

I am using JavaScript’s replace() function to replace a regular expression matching A-Z, a-z, 0-9, dash (-), underscore (_) and dot (.). I am trying to replace it with part of the matched string with tags around it, but the code I have only replaces the matches with: <b>$1</b>.

Any help, please?

<!DOCTYPE html>
<html>
<body>

<textarea id="demo" rows="10" cols="20">
<table></table>
<b><i>#stock-jsod.20</i>
</b>
</textarea>

<button onclick="testit()">test</button>

<script type="text/javascript">

function testit()
{
var str=document.getElementById("demo").value;
var n = str.replace(/#([A-Za-z0-9_.-]+)/gi, "<b>$0</b>");
document.getElementById("demo").innerHTML=n;
}
</script>

</body>
</html>

My goal here is to replace #stock-jsod.20 with <b>#stock-jsod.20</b>, but currently it only replaces it with <b>$0</b>.

  • 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-06T20:08:05+00:00Added an answer on June 6, 2026 at 8:08 pm

    In addition to Engineer’s answer you also don’t the A-Za-z0-9_. You can just use \w. Then you also don’t need the case insensitive switch. This is a much simpler regular expression that achieves the same effect:

    var n = str.replace(/#[\w.-]+/g, "<b>$&</b>");
    

    Example output:

    > str = '#abc #def ghi'
    "#abc #def ghi"
    > str.replace(/#[\w.-]+/g, "<b>$&</b>");
    "<b>#abc</b> <b>#def</b> ghi"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using JavaScript I'm trying to split a paragraph into it's sentences using regular expressions.
I'm using Javascript sort (with Underscore.js): _.sortBy([Bob, Mary, Alice], function (name) {return name}) >
Using javascript, how do I add some data to the query string? Basically I
Using javascript, I have a file in string (got with ajax request). How to
Using javascript I'm trying to load images not on the current page (from an
Using Javascript, I want to take the content of a string and wrap every
Using javascript, I am trying to change the selection of the listbox item like
Using JavaScript, I need to check if a given string contains a sequence of
Using Javascript. I'm trying to loop through an array encoded with JSON. Here is
Using Javascript, is there an equivalent function or functionality so VBScript's IsEmpty function? The

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.