I\'m trying to figure out how to match the following strings:
blaOSXbla
os X bla
bla os-x
blablaOS_Xbla
A common pattern is pretty easy:
Try:
/os[ \-_]?x)/i.test(string)
Use ?
to match the previous set zero or one time.
Demo
You need to use a character class:
/os[ _-]?x/i
See demo
With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. Simply place the characters you want to match between square brackets. If you want to match an
a
or ane
, use[ae]
.A character class matches only a single character.
gr[ae]y
does not matchgraay
,graey
or any such thing. The order of the characters inside a character class does not matter. The results are identical.
And you need ?
to make the match optional.
See "Optional Items":
The question mark makes the preceding token in the regular expression optional.
colou?r
matches bothcolour
andcolor
. The question mark is called a quantifier.