正则表达式,替换所有以@开头的单词 [英] Regex, replace all words starting with @

查看:86
本文介绍了正则表达式,替换所有以@开头的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个正则表达式,将以@开头的所有单词放入span标记中. 我已经完成了所需的工作,但是不确定我是否完全了解自己在这里所做的事情.

I have this regular expression that puts all the words that starts with @ into span tags. I've accomplished what is needed but i'm not sure that i completely understand what i did here.

content.replace(/(@\S+)/gi,"<span>$1</span>")

  1. ()意味着匹配整个单词,对吧?
  2. @表示以@开头.
  3. \ S的意思是紧跟空格之前的所有内容".

但是,如果不在\ S后面加+号,那么它仅与第一个字母匹配,怎么会这样呢?

But how come that if don't add the + sign after the \S , it matches only the first letter?

任何输入将不胜感激.

推荐答案

\S是任何非空白字符,并且a+表示一个或多个.所以

\S is any non-whitespace character and a+ means one or more of a. So

@\S->一个@,后跟一个非空格字符.

@\S -> An @ followed by one non-whitespace character.

@\S+->一个@,后跟一个或多个非空白字符

@\S+ -> An @ followed by one or more non-whitespace characters

这篇关于正则表达式,替换所有以@开头的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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