Skip to content

Instantly share code, notes, and snippets.

@aquarla
Created May 2, 2022 09:20
Show Gist options
  • Select an option

  • Save aquarla/ef6c351fb1e23b09961b1b092e740a61 to your computer and use it in GitHub Desktop.

Select an option

Save aquarla/ef6c351fb1e23b09961b1b092e740a61 to your computer and use it in GitHub Desktop.
テキストからハッシュタグを抽出
const text = "#テ・スト 今日はカレーです #あbc #aaa  #gochisou_photo abcde";
const WORD = '\\p{L}\\p{M}\\p{N}\\p{Connector_Punctuation}';
const ALPHA = '\\p{L}\\p{M}';
const SEPARATORS = '\\u00b7\\u2007';
const regexp = new RegExp(`(?:^|[^\/\)\w])(#(([${WORD}_][${WORD}${SEPARATORS}]*[${ALPHA}${SEPARATORS}][${WORD}${SEPARATORS}]*[${WORD}_])|([${WORD}_]*[${ALPHA}][${WORD}_]*)))`, 'gu');
const tags = Array.from(text.matchAll(regexp)).map((item) => item[1]);
console.log(tags);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment