最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove hashtag symbol js, by regex - Stack Overflow

programmeradmin1浏览0评论

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);

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

Share Improve this question edited Sep 13, 2018 at 12:56 Bram Vanroy 28.7k27 gold badges147 silver badges265 bronze badges asked Sep 13, 2018 at 12:51 StasStas 6956 silver badges22 bronze badges 1
  • 1 Remember when "the hashtag symbol" was just called "hash"? Pepperidge farm remembers. – Joachim Sauer Commented Sep 13, 2018 at 12:57
Add a ment  | 

4 Answers 4

Reset to default 5

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.

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'

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.

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

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

This will replace all #

发布评论

评论列表(0)

  1. 暂无评论