admin管理员组

文章数量:1403500

I know this can quickly be done with Regex:

I got string like:

"Alpha OmegaS Sheol Gehena GSSaga Serekali"

I wanna remove words that Starts with s.

So I should have:

"Alpha OmegaS Gehena GSSaga"

What have I tried?

Something like: str.replace(/^\\S/,"") //This NO GOOD.

The thing is I understand REGEX very well, but somehow REGEX does NOT understand me.

Any help is appreciated.

I know this can quickly be done with Regex:

I got string like:

"Alpha OmegaS Sheol Gehena GSSaga Serekali"

I wanna remove words that Starts with s.

So I should have:

"Alpha OmegaS Gehena GSSaga"

What have I tried?

Something like: str.replace(/^\\S/,"") //This NO GOOD.

The thing is I understand REGEX very well, but somehow REGEX does NOT understand me.

Any help is appreciated.

Share Improve this question asked Sep 3, 2013 at 11:01 ErickBestErickBest 4,6905 gold badges32 silver badges45 bronze badges 2
  • You specified a lower case "s" where as the text sample you give only contains words beginning with an uppercase "S". Are you looking to remove words starting with both lower and uppercase "s"? – Lix Commented Sep 3, 2013 at 11:09
  • insensitively targeting the case will be a prudent move... – ErickBest Commented Sep 3, 2013 at 11:16
Add a ment  | 

1 Answer 1

Reset to default 6

How about:

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

Explanation:

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

本文标签: