javascript how to regex match and replace on hashtags but exclude the hashtag character

javascript regex multiple matches
regex matching hashtag
javascript pattern matching
regular expression match
javascript find hashtags in string
javascript regex test
jquery regex match
remove hashtags from string python

I have the following function:

function formattedTitle(posttitle,hreflink) {
  return `<a href='`+ hreflink +`'>` + posttitle.replace(/(^|\s)(#[-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>$2</a><a href='`+ hreflink + `'>`) + '</a>';

When I run

console.log(formattedTitle('This is #awesome news',''));

It outputs:

<a href=''>This is </a><a class="hashtag" href='/search?q=hashtag:"#awesome"'>#awesome</a><a href=''> news</a>

function formattedTitle(posttitle, hreflink) {
  return `<a href='` + hreflink + `'>` + posttitle.replace(/(^|\s)(#[-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>$2</a><a href='` + hreflink + `'>`) + '</a>';

console.log(formattedTitle('This is #awesome news', ''));

Move the # outside of the captured 2nd group, so it isn't captured. When replacing, in the href, replace with just $2 (so no hashtag). When replacing the text inside the <a>, replace with #$2, so that the hashtag gets added in the proper place:

function formattedTitle(posttitle, hreflink) {
  return `<a href='` + hreflink + `'>`
    + posttitle.replace(/(^|\s)#([-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>#$2</a><a href='` + hreflink + `'>`)
    + '</a>';

console.log(formattedTitle('This is #awesome news', ''));

Javascript/jQuery, Here's a regex to match hashtag(#) if it has a space before it or it's beginning of string.. like so: (^|\s)(#[a-z\d-]+). Working regex example:. It will exclude hashtags that are separated by spaces ("hey there #friend" fails to match "#friend"). It doesn't allow you to place a min/max length on the hashtag. It doesn't offer a lot of flexibility if you decide to add other symbols/characters to your valid input list.

You just need to move the hash symbol out of your capture group; doing so will not change the matching semantics. Like so:

function formattedTitle(posttitle, hreflink) {
  return `<a href='`+ hreflink +`'>` + posttitle.replace(/(^|\s)#([-.\w]+)/gi, `$1</a><a class="hashtag" href='/search?q=hashtag:"$2"'>$2</a><a href='`+ hreflink + `'>`) + '</a>';

Remove all but specified hashtags at the end of a string using regex , I have a regex expression that almost works as I'd like, but I need advice on how to intact, and all hashtags at the end (except the excluded ones) to be removed​. Remove if you want to match any hashtags but those that start with hashtag or This avoids the problem by just replacing trailing non-word characters without  It seems like the dot character in Javascript’s regular expressions matches any character except new line and no number of modifiers could change that. Sometimes you just want to match everything and there’s a couple of ways to do that. You can pick an obscure character and apply a don’t match character range with it ie [^`]+. This is not

Just add another capturing group so that you get a match with the hashtag (or any other character you'd like to capture) in $2 and another match without the hashtag in $3: (#([-.\w]+)) rather than (#[-.\w]+):

function formattedTitle(postTitle, href) {
  const parts = postTitle.replace(
    `$1</a><a class="hashtag" href="/search?q=hashtag:$3">$2</a><a href="${ href }">`);
  return `<a href="${ href }">${ parts }</a>`;

document.getElementById('postTitle').innerHTML = formattedTitle('This is #awesome news', '');
h3 {
  font-family: monospace;
  font-size: 24px;
  margin: 8px 0;

a {
  padding: 8px 0;
  text-decoration: none;
  display: inline-block;

.hashtag {
  padding: 6px 8px;
  border: 3px solid blue;
  border-radius: 3px;
  margin: 0 8px;
<h3 id="postTitle"></h3>

Hashtag RegEx, Toggle navigation. RegEx Pal From Dan's Tools. Web Dev. HTML/JS/CSS Playground · HTML Color Codes · CSS Fonts · Online Diff Tool .htaccess Generator  Find out the proper way to replace all occurrences of a string in plain JavaScript, from regex to other approaches Published Jul 02, 2018 , Last Updated Sep 29, 2019 Using a regular expression

Hashtag RegEx, Toggle navigation. RegEx Testing From Dan's Tools. Web Dev. HTML/JS/CSS Playground · HTML Color Codes · CSS Fonts · Online Diff Tool .htaccess  In JavaScript, regular expressions are often used with the two string methods: search () and replace (). The search () method uses an expression to search for a match, and returns the position of the match. The replace () method returns a modified string where the pattern is replaced. Using String search () With a String

need regex help in how to remove specifc hashtag but keep other , I used the regex to get all hashtags from the tweet, but I need to exclude the tag that contains Negative lookaround might be what you need to guarantee your matches don't contain "ama" Regex Search and Replace Puzzle with LookArounds and Group Capturing Gets all the characters except the comma into group 1. String.prototype.replace can take RegEx as the first argument and a function as the second. This callback function can receive many arguments, but the first is the matched text itself. This means we can use the original urls/time/hash in our replacement string. The idea is to replace our matches with the appropriate HTML. To keep things simple, we'll start with urls.

Code: Regex for Instagram Username and Hashtags - Thoughts, Here is a regex to capture and validating them with the JavaScript regex engine. Matches with one . in them @disco.dude but not two . The next two columns work hand in hand: the "Example" column gives a valid regular expression that uses the element, and the "Sample Match" column presents a text string that could be matched by the regular expression.

  • Didn't realize it would be so simple! Thank you, works perfectly!