A pot
Rose
A pot
Primrose
A pot
Rose
A pot
Rosemary
A pot
Rose
1
let garden = "Rose\tPrimrose\tRose\tRosemary\tRose";
123456
garden = garden
.replace(//gi, "Blackberry");
Lesson 29 / 42

Lesson task •

Replace all the roses with blackberries

Word boundary

\b

Likewise we restricted before the regular expressions to match start of string and end of string, we can make regular expressions to match a word boundary:

123
console.log("foointhestart endswithfoo foobothsidesfoo foo".replace(/\bfoo/gi, "bar")) // barinthestart endswithfoo barbothsidesfoo bar 
console.log("foointhestart endswithfoo foobothsidesfoo foo".replace(/foo\b/gi, "bar")) // foointhestart endswithbar foobothsidesbar bar 
console.log("foointhestart endswithfoo foobothsidesfoo foo".replace(/\bfoo\b/gi, "bar")) // foointhestart endswithfoo foobothsidesfoo bar 

The word boundary is defined as the transition from any symbol matching \w to any symbol matching \W:

1234
console.log("foo42".replace(/foo\b/gi, "bar")) // foo42 
console.log("foo_".replace(/foo\b/gi, "bar")) // foo_ 
console.log("foo$".replace(/foo\b/gi, "bar")) // bar$ 
console.log("foo+-".replace(/foo\b/gi, "bar")) // bar+-