RegexpGarden
















- Garden Text Representation
1234
let garden = "Rosemary\tRose\tPrimrose\t
Strawberry\tBlackberry\t
Rose\tBlackberry\t
Strawberry";
- Code Editor
- Log
123456
garden = garden
.replace(//gi, "Blackberry");
Lesson task •
Replace every rose or strawberry with blackberry
Disjunction
|
x|y matches either "x" or "y"123
console.log(!!"foo".match(/foo|bar/)) // true
console.log(!!"bar".match(/foo|bar/)) // true
console.log(!!"baz".match(/foo|bar/)) // false
To use a disjunction as part of the regular expression wrap it into a non-capturing group:
123456
console.log(!!"foo".match(/(?:foo|bar)42/)) // false
console.log(!!"foo42".match(/(?:foo|bar)42/)) // true
console.log(!!"bar".match(/(?:foo|bar)42/)) // false
console.log(!!"bar42".match(/(?:foo|bar)42/)) // true
console.log(!!"baz".match(/(?:foo|bar)42/)) // false
console.log(!!"baz42".match(/(?:foo|bar)42/)) // false
Fun fact: you can rewrite a custom symbol class with a disjunction: /(?:x|y)/ equals to /[xy]/:
12345678910
console.log(!!":".match(/[:;]/)) // true
console.log(!!":".match(/(?::|;)/)) // true
console.log(!!";".match(/[:;]/)) // true
console.log(!!";".match(/(?::|;)/)) // true
console.log(!!"4".match(/[:;]/)) // false
console.log(!!"4".match(/(?::|;)/)) // false
console.log(!!"f".match(/[:;]/)) // false
console.log(!!"f".match(/(?::|;)/)) // false
console.log(!!":::".match(/^[:;]$/)) // false
console.log(!!":::".match(/^(?::|;)$/)) // false