[英]javascript extract hashtags from strings
I have a string received from backend, and I need to extract hashtags. 我有一个从后端收到的字符串,我需要提取主题标签。 The tags are written in one of these two forms 标签以以下两种形式之一书写
type 1. #World is a #good #place to #live.
type 2. #World#place#live.
I managed to extract from first type by : str.replace(/#(\\S*)/g
how can i change the second format to space seperated tags as well as format one? 我设法通过以下方式从第一种类型中提取: str.replace(/#(\\S*)/g
我如何才能将第二种格式更改为以空格分隔的标记以及第一种格式?
basically i want format two to be converted from 基本上我想从格式2转换为
#World#place#live.
to 至
#World #place #live.
You can use String.match , with regex #\\w+
: 您可以将String.match与正则表达式#\\w+
:
var str = ` type 1. #World is a #good #place to #live. type 2. #World#place#live.` var matches = str.match(/#\\w+/g) console.log(matches)
\\w+
matches any word character [a-zA-Z0-9_] more than once, so you might want to tweak that. \\w+
多次匹配任何单词字符[a-zA-Z0-9_],因此您可能需要对其进行调整。
Once you have the matches in an array you can rearrange them to your likes. 将比赛安排在一个数组中后,您可以将其重新排列为自己喜欢的。
The pattern #(\\S*)
will match a #
followed by 0+ times a non whitespace character in a captured group. 模式#(\\S*)
将与#
匹配,后跟0+乘以捕获组中的非空白字符。 That would match a single # as well. 那也将匹配一个#。 The string #World#place#live.
字符串#World#place#live.
contains no whitespace character so the whole string will be matched. 不包含空格字符,因此整个字符串将被匹配。
You could match them instead by using a negated character class. 您可以使用否定的字符类来匹配它们。 Match #, followed by a negated character class that matches not a # or a whitespace character. 匹配#,然后是与#或空格字符不匹配的否定字符类。
#[^#\s]+
const strings = [ "#World is a #good #place to #live.", "#World#place#live." ]; let pattern = /#[^#\\s]+/g; strings.forEach(s => { console.log(s.match(pattern)); });
How about that using regex /#([\\w]+\\b)/gm
and join by space 使用regex /#([\\w]+\\b)/gm
并按空格连接怎么样 like below to extract
#hastags
from your string? 像下面#hastags
从字符串中提取#hastags
? OR you can use str.replace(/\\b#[^\\s#]+/g, " $&")
as commented by @Wiktor 或者您可以使用@Wiktor注释的str.replace str.replace(/\\b#[^\\s#]+/g, " $&")
function findHashTags(str) { var regex = /#([\\w]+\\b)/gm; var matches = []; var match; while ((match = regex.exec(str))) { matches.push(match[0]); } return matches; } let str1 = "#World is a #good #place to #live." let str2 = "#World#place#live"; let res1 = findHashTags(str1); let res2 = findHashTags(str2); console.log(res1.join(' ')); console.log(res2.join(' '));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.