Stas
Stas

Reputation: 695

Remove hashtag symbol js, by regex

Tried to search on the forum but could not find anything that would precisely similar to what i need. Im basically trying to remove the # symbol from results that im receving, here is the dummy example of the regex.

let postText = 'this is a #test of #hashtags';
var regexp = new RegExp('#([^\\s])', 'g');
postText = postText.replace(regexp, '');

console.log(postText);

It gives the following result

this is a est of ashtags

What do i need to change around so that it removes just the hashtags without cutting the first letter of each word

Upvotes: 3

Views: 2877

Answers (4)

Aaron
Aaron

Reputation: 24812

You might be able to use the following :

let postText = 'this is a #test of #hashtags';
postText = postText.replace(/#\b/g, '');

It relies on the fact that a #hashtag contains a word-boundary between the # and the word that follows it. By matching that word-boundary with \b, we make sure not to match single #.

However, it might match a bit more than you would expect, because the definition of 'word character' in regex isn't obvious : it includes numbers (so #123 would be matched) and more confusingly, the _ character (so #___ would be matched).

I don't know if there's an authoritative source defining whether those are acceptable hashtags or not, so I'll let you judge whether this suits your needs.

Upvotes: 1

Maksym Shevchenko
Maksym Shevchenko

Reputation: 746

Probably easier will be to write your own function which probably will look like this: (covers the usecase when symbol may be repeated)

  function replaceSymbol(symbol, string) {
    if (string.indexOf(symbol) < 0) {
      return string;
    }

    while(string.indexOf(symbol) > -1) {
      string = string.replace(symbol, '');
    }

    return string;
  }



var a = replaceSymbol('#', '##s##u#c###c#e###ss is he#re'); // 'success is here'

Upvotes: 1

TheGrep
TheGrep

Reputation: 1

You only need the #, the stuff in parens match anything else after said #

postText = postText.replace('#', '');

This will replace all #

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

You need a backreference $1 as the replacement:

let postText = 'this is a #test of #hashtags';
var regexp = /#(\S)/g;
postText = postText.replace(regexp, '$1');
console.log(postText);
// Alternative with a lookahead:
console.log('this is a #test of #hashtags'.replace(/#(?=\S)/g, ''));

Note I suggest replacing the constructor notation with a regex literal notation to make the regex a bit more readable, and changing [^\s] with a shorter \S (any non-whitespace char).

Here, /#(\S)/g matches multiple occurrences (due to g modifier) of # and any non-whitespace char right after it (while capturing it into Group 1) and String#replace will replace the found match with that latter char.

Alternatively, to avoid using backreferences (also called placeholders) you may use a lookahead, as in .replace(/#(?=\S)/g, ''), where (?=\S) requires a non-whitespace char immediately to the right of the current location. If you need to remove # at the end of the string, too, replace (?=\S) with (?!\s) that will fail the match if the next char is a whitespace.

Upvotes: 5

Related Questions