开发者

lastIndexOf to find 3 consecutive space

I want to find 3 consecutive space in a string.

SO far i've tried

str.indexOf("   ")
str.match(/\s\s\s+/)

none of them worked [both returns 1, and detect single space as 3 space]

Edit

Sample Input:

  Blah blah 1space  2space   3space      lots 开发者_运维知识库of spaceNo space

It breaks^ here and detect it as 3 space [FF 4]


Your regex looks close, except you're searching for \s (which also matches tabs and newlines), and you're searching for 3+ (not exactly 3). Try using this instead:

str.search(/ {3}/)

It'll return the index of the first three consecutive spaces (or -1 if there's no match).

If you want to match three or more spaces, you can use this repetition syntax:

str.search(/ {3,}/)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜