简体   繁体   English

正则表达式以匹配空白之间的字符串(JavaScript)

[英]regex to match a string among white space (JavaScript)

I try to match a string among white spaces. 我尝试在空白之间匹配一个字符串。

I just thought it's as simple as \\S* , but does not work http://regex101.com/r/jC0uA5 我只是以为它就像\\S*一样简单,但是不起作用http://regex101.com/r/jC0uA5

What do I miss?? 我想念什么?

EDIT: 编辑:

I also thought 我还以为

It's as simple as. 就这么简单。

http://regex101.com/r/zD4vN8 http://regex101.com/r/zD4vN8

\S.*\S

but this does match only more than 2 characters. 但这确实只能匹配2个以上的字符。

You almost had it... * matches zero or more characters. 您几乎拥有了... *匹配零个或多个字符。 Try \\S+ 试试\\S+

Simply do: \\S+ 只需执行: \\S+

I am assuming that you do not require whitespace around the string, but that you want to ignore it if it is there. 我假设您不需要在字符串周围使用空格,但是如果有空格,则想忽略它。

Your previous regular expression of \\S* was actually matching, but the first match returned was an empty string. 您以前的\\S*正则表达式实际上是匹配的,但是返回的第一个匹配是一个空字符串。

For clarification, do you wish to also match whitespace within the string, or is your meaning of "string" intended to include only non-whitespace? 为了澄清起见,您是否还希望在字符串中匹配空格,或者“字符串”的含义旨在仅包含非空格?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM