Reputation: 31
As per the title, I'm trying to use a .match() function on a string to only match with instances that occur at the start or end of that string (but no where in the middle).
For example, on the word "test":
x = "1 TEST 1"
x1 = "TEST 1"
x2 = "1 TEST"
x3 = "TEST"
Only x1, x2 and x3 should match the regex. Currently, I have:
.toUpperCase().match(/(TEST)/)
Which will match every instance of "test". I've tried using the ^ and $ modifiers, but they'll only match x3. This one however:`
.toUpperCase().match(/(TEST)$/)
Will match x2 and x3 only, with .toUpperCase().match(/^(TEST)/)
matching x1 and x3 only.
Upvotes: 3
Views: 2734
Reputation: 326
/^TEST.*|.*TEST$/
this regex will match a line if it starts or ends with "TEST". add m
to match multiple lines
Note: this regex will return the whole line
Upvotes: 0
Reputation: 4614
Just match one or the other using |
.
.toUpperCase().match(/^TEST|TEST$/)
Also note that you don't need to put parentheses around TEST
.
Upvotes: 6