In Regular Expression what is the different between \^n and \b

Viewed 46

In Regular Expression

  • /^n/
  • /\b/

I always confuse them

So could you explain to me the difference between them and when to use each of them?

1 Answers

^ means beginning of whole text line.
\b means beginning of word, but line could hold many words, obviously.

// Search for n only at the beginning of LINE
console.log(1, "no yes".match(/^n/));
console.log(2, "yes no".match(/^n/));

// Search for n at the beginning of ALL words in line (with flag g)
// or only for first one without g flag
console.log(3, "yes no yes no".match(/\bn/g));
console.log(4, "yes no yes no".match(/\bn/));

Related