使用regex从复杂字符串中提取主题标签 [英] Extract hashtags from complex string using regex

查看:49
本文介绍了使用regex从复杂字符串中提取主题标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个疯狂的字符串,例如:

I have a crazy string, something like:

sun #plants #!wood% ##arebaba#tey   travel#blessed    #weed das#$#F!@D!AAAA

我想提取所有单词 (也包含特殊字符)以#开头或之前有空格,结果如下:

I want to extract all "words" (also containing special characters) that begin with # or that have a space right before, taking the following as a result:

[
  'sun',
  'plants',
  '!wood%',
  'arebaba',
  'tey',
  'travel',
  'blessed',
  'weed',
  'das',
  '$',
  'F!@D!AAAA'
]

如何使用正则表达式进行此操作?

How do I get this using regex?

推荐答案

您可以使用正则表达式匹配 [^#\ s] +

You can use match using regex: [^#\s]+:

var str = 'sun #plants #!wood% ##arebaba#tey   travel#blessed    #weed das#$#F!@D!AAAA';
    
var arr = str.match(/[^\s#]+/g);

console.log(arr);

RegEx演示

这篇关于使用regex从复杂字符串中提取主题标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆