简体   繁体   中英

Regular expression to match 2 words

I just need to make sure 2 words are entered into a text field. The words can contain any character with any number of spaces in between them. So essentially this format:

<word><any number of spaces><word>

In tried the following and it returns not null for more than 2 words:

'Hello world Something'.match(/(.*){1}\s(.*){1}/)

Any idea what the right REGEX will look like?

My guess is that you might want to design an expression similar to:

^\b[\w-]+\b\s+\b[\w-]+\b$

and change the char classes ( [\\w-] ) as you wish.

DEMO 1

Edit:

^\S+\s+\S+$

DEMO 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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