RegExp must return hashtags with special conditions

I’m not strong in regular expressions, so I need regExp for validating a hashtags. All hashtags should be marked according to the rules:

  • always use the # before your chosen keywords.
  • do not use punctuation or spaces in hashtags.
  • never put characters immediately before the #.
export class AppComponent implements OnInit {
  str: string =
    "#des.ign #living?room #how? #”your”dream #des’ign #new,story ##des-ign #des—ign #new! #des:ign #des;ign #(des#) #[de#s] #new... #des4u/. #12345.";
  text: string = "";
  replaceAll: any;
  constructor() {}
  ngOnInit() {
    this.showText(this.str);
  }
  showText = (str) => {
    const regex = /#^[A-Za-z0-9]/g;
    this.text = str.replace(regex, (value) => `<p>${value}</p>`);
    return this.text;
  };
}

Answer

You can extract all these hashtags first with a pattern like ((?:^|s)#)(?!#*d+.?(?!S))#*([^#s]S*) (see demo) and then post-process matches by removing unexpected chars from Group 1 value, that will be your result.

Note it is not possible to achieve what you need with a single regex matching or replacing operation, so you can use

var string = "#des.ign #living?room #how? #”your”dream #des’ign #new,story ##des-ign #des—ign #new! #des:ign #des;ign #(des#) #[de#s] #new... #des4u/. #12349108.";
var rx = /((?:^|s)#)(?!#*d+.?(?!S))#*([^#s]S*)/g;
var rx_special = /[^A-Za-zd]/g;
console.log(
  string.replace(rx, (m, first, second) => first + second.replace(rx_special, ''))
);

Output:

#design #livingroom #how #yourdream #design #newstory #design #design #new #design #design #des #des #new #des4u #12349108

Regex details

  • ((?:^|s)#) – Group 1: start of string or whitespace and then a #
  • (?!#*d+.?(?!S)) – a negative lookahead that fails the match if there are zero or more # chars, one or more digits, an optional . followed with whitespace or end of string immediately to the right of the current location
  • #* – zero or more # chars
  • ([^#s]S*) – Group 2: any char other than # and whitespace and then any zero or more non-whitespace chars.
  • [^A-Za-zd] – matches any char other than ASCII letter or digit.