With JavaScript RegExp find a character except newline?

To find a character except for a newline, use the dot metacharacter . (period). The dot matches any single character except the newline character (
).

Syntax

/./        // Matches any character except newline
/.+/       // Matches one or more characters except newline
/a.b/      // Matches 'a', any character, then 'b'

Example

<html>
   <head>
      <title>JavaScript Regular Expression</title>
   </head>
   <body>
      <script>
         var myStr = "We provide websites! We provide content!";
         var reg = /p.o/g;
         var match = myStr.match(reg);
         
         document.write(match);
      </script>
   </body>
</html>
pro,pro

How It Works

The pattern /p.o/g matches:

  • p - literal character 'p'
  • . - any single character except newline
  • o - literal character 'o'
  • g - global flag to find all matches

In the string "We provide websites! We provide content!", it finds "pro" from both "provide" instances.

Dot vs Newline Behavior

<html>
   <head>
      <title>Dot Metacharacter Test</title>
   </head>
   <body>
      <script>
         var text = "Line1\nLine2";
         
         // Dot does NOT match newline
         var dotMatch = text.match(/e./g);
         document.write("Dot matches: " + dotMatch + "<br>");
         
         // To match including newlines, use [\s\S]
         var allMatch = text.match(/e[\s\S]/g);
         document.write("Including newlines: " + allMatch);
      </script>
   </body>
</html>
Dot matches: e1
Including newlines: e1,e2

Common Use Cases

  • /a.b/ - Match 'a', any character, then 'b' (matches "axb", "a5b", "a b")
  • /\d.\d/ - Match digit, any character, digit (matches "1.5", "3x7", "2 9")
  • /.{3}/ - Match any 3 characters in sequence

Conclusion

The dot metacharacter . matches any single character except newline. Use it for flexible pattern matching where you need to match "any character" in a specific position.

Updated on: 2026-03-15T23:18:59+05:30

296 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements