DD
DevDash

Regex for LaTeX Command

Regex Pattern

\\[a-zA-Z]+(?:\{[^}]*\})*

LaTeX command with optional braced arguments

Quick Answer

The regex pattern for latex command is `\\[a-zA-Z]+(?:\{[^}]*\})*`. LaTeX command with optional braced arguments. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
\textbf{bold}✓ Matches
\section{Title}✓ Matches
\frac{1}{2}✓ Matches
plain text✗ No match
\✗ No match
{braces}✗ No match

Code Examples

javascript

const regex = /\\[a-zA-Z]+(?:\{[^}]*\})*/;
const isValid = regex.test(value);

python

import re
pattern = r'\\[a-zA-Z]+(?:\{[^}]*\})*'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /\\[a-zA-Z]+(?:\{[^}]*\})*/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/\\[a-zA-Z]+(?:\{[^}]*\})*/', $value)) {
    echo "valid";
}

java

String pattern = "\\\\[a-zA-Z]+(?:\\{[^}]*\\})*";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.