user1028880
user1028880

Reputation:

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

What do I miss??

EDIT:

I also thought

It's as simple as.

http://regex101.com/r/zD4vN8

\S.*\S

but this does match only more than 2 characters.

Upvotes: 0

Views: 48

Answers (2)

Joseph Myers
Joseph Myers

Reputation: 6552

Simply do: \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.

For clarification, do you wish to also match whitespace within the string, or is your meaning of "string" intended to include only non-whitespace?

Upvotes: 0

B.J. Smegma
B.J. Smegma

Reputation: 304

You almost had it... * matches zero or more characters. Try \S+

Upvotes: 1

Related Questions