Javascript / REGEX:删除一个特定的文本(单词),该单词以String中的特定字母开头,单词用空格分隔

时间:2013-09-03 11:01:40

标签: javascript regex string text replace

我知道这可以通过Regex快速完成:

我的字符串如下:

  

“Alpha OmegaS Sheol Gehena GSSaga Serekali”

我想删除以s开头的单词。

所以我应该:

  

“Alpha OmegaS Gehena GSSaga”

我尝试了什么?

类似于:str.replace(/^\\S/,"") //This NO GOOD.

事情是我很了解REGEX,但不知何故REGEX不理解我。

感谢任何帮助。

1 个答案:

答案 0 :(得分:6)

怎么样:

str.replace(/\bs\S+/ig,"")

<强>解释

NODE                     EXPLANATION
----------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
----------------------------------------------------------------------
  s                        's'
----------------------------------------------------------------------
  \S+                      non-whitespace (all but \n, \r, \t, \f,
                           and " ") (1 or more times (matching the
                           most amount possible))
----------------------------------------------------------------------

i is for case-insensitive
g is for global