/coding/
等正则表达式,可以在另一个字符串中查找"coding"
模式。这对搜索单个字符串很有用,但它仅限于一种模式。您可以使用alternation
或OR
运算符搜索多个模式: |
。此运算符在其之前或之后匹配模式。例如,如果你想匹配"yes"
或"no"
,你想要的正则表达式是/yes|no/
。您还可以搜索两种以上的模式。您可以通过添加更多模式来实现此操作,其中更多OR
运算符将它们分开,例如/yes|no|maybe/
。 petRegex
以匹配宠物"dog"
, "cat"
, "bird"
或"fish"
。 petRegex
应该为字符串"John has a pet dog."
返回true
"John has a pet dog."
testString: 'assert(petRegex.test("John has a pet dog."), "Your regex petRegex
should return true
for the string "John has a pet dog."
");'
- text: 你的正则表达式petRegex
应该为字符串"Emma has a pet rock."
返回false
"Emma has a pet rock."
testString: 'assert(!petRegex.test("Emma has a pet rock."), "Your regex petRegex
should return false
for the string "Emma has a pet rock."
");'
- text: 你的正则表达式petRegex
应该为字符串"Emma has a pet bird."
返回true
"Emma has a pet bird."
testString: 'assert(petRegex.test("Emma has a pet bird."), "Your regex petRegex
should return true
for the string "Emma has a pet bird."
");'
- text: 你的正则表达式petRegex
应该返回true
为字符串"Liz has a pet cat."
testString: 'assert(petRegex.test("Liz has a pet cat."), "Your regex petRegex
should return true
for the string "Liz has a pet cat."
");'
- text: 你的正则表达式petRegex
应该返回false
为"Kara has a pet dolphin."
的字符串"Kara has a pet dolphin."
testString: 'assert(!petRegex.test("Kara has a pet dolphin."), "Your regex petRegex
should return false
for the string "Kara has a pet dolphin."
");'
- text: 你的正则表达式petRegex
应该返回true
为字符串"Alice has a pet fish."
testString: 'assert(petRegex.test("Alice has a pet fish."), "Your regex petRegex
should return true
for the string "Alice has a pet fish."
");'
- text: 你的正则表达式petRegex
应该返回false
为字符串"Jimmy has a pet computer."
testString: 'assert(!petRegex.test("Jimmy has a pet computer."), "Your regex petRegex
should return false
for the string "Jimmy has a pet computer."
");'
```