开发者

lastIndexOf to find 3 consecutive space

开发者 https://www.devze.com 2023-03-20 13:13 出处:网络
I want to find 3 consecutive space in a string. SO far i\'ve tried str.indexOf(\"\") str.match(/\\s\\s\\s+/)

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

精彩评论

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