const regex = /\B#([\p{L}\p{N}_]+)(?!#)\b/ug;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('\\B#([\\p{L}\\p{N}_]+)(?!#)\\b', 'ug')
const str = `https://google.com/#test #asdasd
#test
#你好
#asdasdasd .#asdasd #asdasd#asdasd
Works well for most languages. And requires a space in front.
##no <-- not allowed which is good.
#yes
#güneş
#günşöhlesi
Not sure why some say languages like the one above doesn't work...
https://github.com/justoverclockl/flarum-ext-hashtag####hashtag
#asdasd.
#123123123+asdasd`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions