Published on

JS Regular Expression /./g

In JavaScript, the regex:

/./g

means:

  • /.../ → regex literal
  • . → matches any single character (except line breaks by default)
  • gglobal flag, meaning “find all matches”, not just the first one

What it does

It matches every character in a string, one by one.

Example

const str = "hello";
const matches = str.match(/./g);

console.log(matches);
// ["h", "e", "l", "l", "o"]

Notes

  • It does not match newline characters (\n) unless you use the s (dotAll) flag:

    /./gs
    
  • Without g, it would only return the first character:

    "hello".match(/./) // ["h"]