We help IT Professionals succeed at work.

extend text.match for multi words

willa666
willa666 asked
on
if (text.match(/^\/hello$/)) {

How can i best extend the above text.match so that it looks for a 2 or 3 word phrase like"hello world"?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
if (text.match(/^\/hello world$/)) {

Open in new window


or

if (text.match(/^\/hello +world$/)) {  // for one or more spaces

Open in new window


or

if (text.match(/^\/hello\s+world$/)) {  // for one or more whitespaces (i.e. tab, space, newline, etc.)

Open in new window

Author

Commented:
looks great!

at the moment it will get invoked by teh text "/hello". is there a way to do it so it does it for "hello" ?
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Remove the leading "\/".

if (text.match(/^hello$/)) {

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.