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

Time:09-11

In Regular Expression

  • /^n/ Matches any string with n at the beginning of it.
  • /\b/ Find a match at the beginning/end of a word.

I always confuse them

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

CodePudding user response:

^ 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