最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavascriptREGEX: Delete a specific Text (word) starting with a specific letter inside a String with words separated by spaces -

programmeradmin4浏览0评论

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论