Reputation: 13910
I need to match "test"
or "test,"
from a string like
"test, testhellowowo, wowtest,"
Attempt 1
test[,]?
it will match all three since all of the string include the test
regardless what comes after.
Attempt 2
^test[,]?$
it won't match any unless the string is test
or test,
Expected results
"test, testhellowowo, wowtest,".match(x) // should match `test,`
"testhellowowo, wowtest,".match(x) // should not match anything
Upvotes: 1
Views: 36
Reputation: 454
If you need to include the punctuation mark you could also do /^test[,]?$/gm
^ matches beginning of string $ matches end of string
Upvotes: 0
Reputation: 318302
You can use word boundaries
/\btest\b/g
They match at
Upvotes: 3