DD
DevDash

Regex for LaTeX Inline Math

Regex Pattern

\$[^$]+\$

LaTeX inline math delimited by single dollar signs

Quick Answer

The regex pattern for latex inline math is `\$[^$]+\$`. LaTeX inline math delimited by single dollar signs. This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
$x^2$✓ Matches
$E = mc^2$✓ Matches
$\alpha + \beta$✓ Matches
$$display$$✗ No match
no math✗ No match
$✗ No match

Code Examples

javascript

const regex = /\$[^$]+\$/;
const isValid = regex.test(value);

python

import re
pattern = r'\$[^$]+\$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /\$[^$]+\$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/\$[^$]+\$/', $value)) {
    echo "valid";
}

java

String pattern = "\\$[^$]+\\$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.