Find #hashtag from string in node js for e.g:
my String is:
string = "I Love #भारत #ভারত #ભારત #ਭਾਰਤ #ଭାରତ #இந்தியா #ഇന്ത്യ #ಭಾರತ #భారత #india and the whole country."
Output:
hashtag = ["भारत", "ভারত", "ભારત", "ਭਾਰਤ", "ଭାରତ", "இந்தியா", "ഇന്ത്യ", "ಭಾರತ", "భారత", "india"]
also i need to replace string in node js for e.g.
Input
String = "I Love #भारत #ভারত #ભારત #ਭਾਰਤ #ଭାରତ #இந்தியா #ഇന്ത്യ #ಭಾರತ #భారత #india and the whole country.";
Output
String = "I Love <a href='http://hostname/hashtag/भारत' target='_blank'>#भारत</a> <a href='http://hostname/hashtag/ভারত' target='_blank'>#ভারত</a> <a href='http://hostname/hashtag/ભારત' target='_blank'>#ભારત</a> <a href='http://hostname/hashtag/ਭਾਰਤ' target='_blank'>#ਭਾਰਤ</a> <a href='http://hostname/hashtag/ଭାରତ' target='_blank'>#ଭାରତ</a> <a href='http://hostname/hashtag/இந்தியா' target='_blank'>#இந்தியா</a> <a href='http://hostname/hashtag/ഇന്ത്യ' target='_blank'>#ഇന്ത്യ</a> <a href='http://hostname/hashtag/ಭಾರತ' target='_blank'>#ಭಾರತ</a> <a href='http://hostname/hashtag/భారత' target='_blank'>#భారత</a> <a href='http://hostname/hashtag/india' target='_blank'>#india</a> and the whole country.";
Here is your string:
const str= "I Love #भारत #ভারত #ભારત #ਭਾਰਤ #ଭାରତ #இந்தியா #ഇന്ത്യ #ಭಾರತ #భారత #india and the whole country.";
To Match hashtags from string.
you can use match
method of string. Here is the reference.
str.match(/(#\S*)/g);
Your code becomes:
const str = "I Love #भारत #ভারত #ભારત #ਭਾਰਤ #ଭାରତ #இந்தியா #ഇന്ത്യ #ಭಾರತ #భారత #india and the whole country.";
const result = str.match(/(#\S*)/g).map(hash=>hash.substr(1));
console.log(result);
To replace hashtags with links.
For the hashtag replacement, you can use the same regex with replace
method of the string.
Here is your code:
const str = "I Love #भारत #ভারত #ભારત #ਭਾਰਤ #ଭାରତ #இந்தியா #ഇന്ത്യ #ಭಾರತ #భారత #india and the whole country.";
const result = str.replace(/(#\S*)/g, (hashtag) => {
const hash=hashtag.substr(1);
return `<a href='http://hostname/hashtag/${hash}' target='_blank'>${hash}</a>`
});
console.log(result);